// 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; using System.Text; using System.Web.Script.Serialization; using System.Xml.Serialization; namespace Test1 { [XmlRoot(ElementName="t1")] public class T1 { public int f1 { get; set; } public string f2 { get; set; } public T1() : this(0, "") { } public T1(int f1, string f2) { this.f1 = f1; this.f2 = f2; } public override string ToString() { return string.Format("{{{0},{1}}}", f1, f2); } } [XmlRoot(ElementName="t1array")] public class T1Array { [XmlElement(ElementName="t1")] public T1[] t1array { get; set; } public T1Array() : this(new T1[0]) { } public T1Array(T1[] array) { this.t1array = array; } } public static class Util { public static string Object2XML(object o) { XmlSerializer ser = new XmlSerializer(o.GetType()); using(StringWriter sw = new StringWriter()) { ser.Serialize(sw, o); return sw.ToString().Replace("utf-16", "utf-8"); // StringWriter end up with "utf-16" which is not good } } public static T XML2Object(string xml) { XmlSerializer ser = new XmlSerializer(typeof(T)); using(StringReader sr = new StringReader(xml)) { return (T)ser.Deserialize(sr); } } public static string Object2JSON(object o) { JavaScriptSerializer ser = new JavaScriptSerializer(); StringBuilder sb = new StringBuilder(); ser.Serialize(o, sb); return sb.ToString(); } public static T JSON2Object(string xml) { JavaScriptSerializer ser = new JavaScriptSerializer(); return (T)ser.Deserialize(xml, typeof(T)); } public static string Object2String(object o, string typ) { if(typ == "application/xml") { return Util.Object2XML(o); } else if(typ == "application/json") { return Util.Object2JSON(o); } else { throw new Exception("Unknown type: " + typ); } } public static T String2Object(string s, string typ) { if(typ == "application/xml") { return Util.XML2Object(s); } else if(typ == "application/json") { return Util.JSON2Object(s); } else { throw new Exception("Unknown type: " + typ); } } public static string GetAllLines(Stream stm) { using(StreamReader sr = new StreamReader(stm)) { return sr.ReadToEnd(); } } } public class ClientManual { private static void TestGetOne(string urlprefix, int f1, string acctyp) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1/" + f1); request.Method = WebRequestMethods.Http.Get; request.Accept = acctyp; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); string contyp = response.ContentType.Split(';')[0]; string respbody = Util.GetAllLines(response.GetResponseStream()); Console.WriteLine(Util.String2Object(respbody, contyp)); response.Close(); } private static void TestGetAll(string urlprefix, string acctyp) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1"); request.Method = WebRequestMethods.Http.Get; request.Accept = acctyp; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); string contyp = response.ContentType.Split(';')[0]; string respbody = Util.GetAllLines(response.GetResponseStream()); T1[] a; if(acctyp == "application/xml") { a = Util.String2Object(respbody, contyp).t1array; } else { a = Util.String2Object(respbody, contyp); } foreach(T1 o in a) Console.Write(o); Console.WriteLine(); response.Close(); } private static void TestGetSome(string urlprefix, int start, int finish, string acctyp) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1?start=" + start + "&finish=" + finish); request.Method = WebRequestMethods.Http.Get; request.Accept = acctyp; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); string contyp = response.ContentType.Split(';')[0]; string respbody = Util.GetAllLines(response.GetResponseStream()); T1[] a; if(acctyp == "application/xml") { a = Util.String2Object(respbody, contyp).t1array; } else { a = Util.String2Object(respbody, contyp); } foreach(T1 o in a) Console.Write(o); Console.WriteLine(); response.Close(); } private static void TestPost(string urlprefix, string contyp, T1 o, string acctyp) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1"); request.Method = WebRequestMethods.Http.Post; request.Accept = acctyp; request.ContentType = contyp; string reqbody = Util.Object2String(o, contyp); request.GetRequestStream().Write(Encoding.UTF8.GetBytes(reqbody), 0, Encoding.UTF8.GetByteCount(reqbody)); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); string contyp2 = response.ContentType.Split(';')[0]; string respbody = Util.GetAllLines(response.GetResponseStream()); Console.WriteLine(Util.String2Object(respbody, contyp2)); response.Close(); } private static void TestPut(string urlprefix, string contyp, T1 o) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1/" + o.f1); request.Method = WebRequestMethods.Http.Put; request.ContentType = contyp; string reqbody = Util.Object2String(o, contyp); request.GetRequestStream().Write(Encoding.UTF8.GetBytes(reqbody), 0, Encoding.UTF8.GetByteCount(reqbody)); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); response.Close(); } private static void TestDelete(string urlprefix, int f1) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlprefix + "/t1/" + f1); request.Method = "DELETE"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Console.WriteLine((int)response.StatusCode / 100 == 2); response.Close(); } private static void Test(string urlprefix, string typ) { Console.WriteLine("**** " + urlprefix + " (" + typ + ") ****"); TestGetOne(urlprefix, 123, typ); TestGetAll(urlprefix, typ); TestGetSome(urlprefix, 10, 12, typ); TestPost(urlprefix, typ, new T1(123, "ABC"), typ); TestPut(urlprefix, typ, new T1(123, "ABC")); TestDelete(urlprefix, 123); } private static void Test(string urlprefix) { Test(urlprefix, "application/xml"); Test(urlprefix, "application/json"); } public static void Main(string[] args) { Test("http://localhost:8080/wsrest/testapi1"); Test("http://localhost:8080/wsrest/testapi2"); Test("http://localhost:8080/wsrest/testapi3"); Test("http://localhost/testapi.ashx"); Test("http://localhost/testapi.svc"); Test("http://localhost/testapi"); Test("http://localhost:81/testapi.php"); } } }