// 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; using System.Collections.Generic; using System.Runtime.Serialization; using RestSharp; namespace Test { [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); } } public class ClientRestSharp { private static void TestGetOne(string urlprefix, int f1, string acctyp) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1/" + f1); request.AddOrUpdateHeader("Accept", acctyp); T1 o = client.GetAsync(request).Result; Console.WriteLine(o); } private static void TestGetAll(string urlprefix, string acctyp) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1"); request.AddOrUpdateHeader("Accept", acctyp); List res = client.GetAsync>(request).Result; foreach(T1 o in res) Console.Write(o); Console.WriteLine(); } private static void TestGetSome(string urlprefix, int start, int finish, string acctyp) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1?start=" + start + "&finish=" + finish); request.AddOrUpdateHeader("Accept", acctyp); List res = client.GetAsync>(request).Result; foreach (T1 o in res) Console.Write(o); Console.WriteLine(); } private static void TestPost(string urlprefix, string contyp, T1 o, string acctyp) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1"); if (contyp == "application/xml") { request.AddXmlBody(o); } else { request.AddJsonBody(o); } request.AddOrUpdateHeader("Accept", acctyp); request.AddOrUpdateHeader("Content-Type", contyp); T1 o2 = client.PostAsync(request).Result; Console.WriteLine(o2); } private static void TestPut(string urlprefix, string contyp, T1 o) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1/" + o.F1); if (contyp == "application/xml") { request.AddXmlBody(o); } else { request.AddJsonBody(o); } request.AddOrUpdateHeader("Content-Type", contyp); RestResponse response = client.PutAsync(request).Result; Console.WriteLine(response.StatusCode); } private static void TestDelete(string urlprefix, int f1) { RestClientOptions options = new RestClientOptions(urlprefix); RestClient client = new RestClient(options); RestRequest request = new RestRequest("/t1/" + f1); RestResponse response = client.DeleteAsync(request).Result; Console.WriteLine(response.StatusCode); } 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:81/testapi_slim.php"); } } }