// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.IO; using System.Net; namespace TextSearch.Solr.HttpPost { public class Program { private static string Http(string method, string url, string typ, string body) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = method; req.Accept = typ; if (body != null) { req.ContentType = typ; StreamWriter sw = new StreamWriter(req.GetRequestStream()); sw.WriteLine(body); sw.Flush(); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if (((int)resp.StatusCode) / 100 == 2) { StreamReader sr = new StreamReader(resp.GetResponseStream()); string content = sr.ReadToEnd(); sr.Close(); return content; } else { Console.WriteLine("Error: {0} {1}", resp.StatusCode, resp.StatusDescription); return null; } } private static string Create(int id, string names, string moves) { return string.Format("{{ 'id': {0}, 'names': '{1}', 'moves': '{2}' }}", id, names, moves); } public static void Main(string[] args) { Http("POST", "http://localhost:8888/solr/chessopening/update?commit=true", "application/json", "{ 'delete': { 'query': '*:*' } }"); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(1, "Spanish defense, Ruy Lopez", "1. e4, e5, 2. Nf3, Nc6, 3. Bb5,")); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(2, "French defense", "1. e4, e6, 2. d4, d5,")); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(3, "Sicilian defense", "1. e4, c5,")); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(4, "Queens gambit", "1. d4, d5, 2. c4,")); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(5, "Dutch defense", "d4, f5,")); Http("POST", "http://localhost:8888/solr/chessopening/update/json/docs?commit=true", "application/json", Create(6, "Reti opening", "Nf3, d5, 2. c4,")); } } }