// 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.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.Serialization; namespace Test2 { [DataContract(Name="t1",Namespace="")] public class T1 { [DataMember(Name="f1")] public int F1 { get; set; } [DataMember(Name="f2")] 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); } } [CollectionDataContract(Name="t1array",Namespace="")] public class T1Array : List { public T1Array() : this(new T1[0]) { } public T1Array(T1[] array) : base(array) { } } public class ClientWebApi { private static void TestGetOne(string urlprefix, int f1, string acctyp) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acctyp)); HttpResponseMessage response = client.GetAsync(urlprefix + "/t1/" + f1).Result; Console.WriteLine(response.IsSuccessStatusCode); Console.WriteLine(response.Content.ReadAsAsync().Result); } private static void TestGetAll(string urlprefix, string acctyp) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acctyp)); HttpResponseMessage response = client.GetAsync(urlprefix + "/t1").Result; Console.WriteLine(response.IsSuccessStatusCode); T1[] a; if(acctyp == "application/xml") { a = response.Content.ReadAsAsync().Result.ToArray(); } else { a = response.Content.ReadAsAsync().Result; } foreach(T1 o in a) Console.Write(o); Console.WriteLine(); } private static void TestGetSome(string urlprefix, int start, int finish, string acctyp) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acctyp)); HttpResponseMessage response = client.GetAsync(urlprefix + "/t1?start=" + start + "&finish=" + finish).Result; Console.WriteLine(response.IsSuccessStatusCode); T1[] a; if(acctyp == "application/xml") { a = response.Content.ReadAsAsync().Result.ToArray(); } else { a = response.Content.ReadAsAsync().Result; } foreach(T1 o in a) Console.Write(o); Console.WriteLine(); } private static void TestPost(string urlprefix, string contyp, T1 o, string acctyp) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acctyp)); HttpResponseMessage response; if(contyp == "application/xml") { response = client.PostAsXmlAsync(urlprefix + "/t1", o).Result; } else { response = client.PostAsJsonAsync(urlprefix + "/t1", o).Result; } Console.WriteLine(response.IsSuccessStatusCode); Console.WriteLine(response.Content.ReadAsAsync().Result); } private static void TestPut(string urlprefix, string contyp, T1 o) { HttpClient client = new HttpClient(); HttpResponseMessage response; if(contyp == "application/xml") { response = client.PutAsXmlAsync(urlprefix + "/t1/" + o.F1, o).Result; } else { response = client.PutAsJsonAsync(urlprefix + "/t1/" + o.F1, o).Result; } Console.WriteLine(response.IsSuccessStatusCode); } private static void TestDelete(string urlprefix, int f1) { HttpClient client = new HttpClient(); HttpResponseMessage response = client.DeleteAsync(urlprefix + "/t1/" + f1).Result; Console.WriteLine(response.IsSuccessStatusCode); } 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"); } } }