Asp.net mvc 4中出现的web api是用来实现REST.
关于什么是REST,可以看这里
通过ajax等访问 web api非常方便,但是如何在Winform, Silverlight等访问web api呢?
通过搜索,发现了已经有人做过这个东西了,就是RestSharp.
RestSharp不只是访问web api, 访问其他平台的Rest API也是一样。
看看介绍的使用,无论是post数据,文件,格式化返回数据,异步请求都非常方便:
var client = new RestClient("http://example.com");// client.Authenticator = new HttpBasicAuthenticator(username, password);var request = new RestRequest("resource/{id}", Method.POST);request.AddParameter("name", "value"); // adds to POST or URL querystring based on Methodrequest.AddUrlSegment("id", 123); // replaces matching token in request.Resource// add parameters for all properties on an objectrequest.AddObject(object);// or just whitelisted propertiesrequest.AddObject(object, "PersonId", "Name", ...);// easily add HTTP Headersrequest.AddHeader("header", "value");// add files to upload (works with compatible verbs)request.AddFile(path);// execute the requestRestResponse response = client.Execute(request);var content = response.Content; // raw content as string// or automatically deserialize result// return content type is sniffed but can be explicitly set via RestClient.AddHandler();RestResponseresponse2 = client.Execute (request);var name = response2.Data.Name;// or download and save file to diskclient.DownloadData(request).SaveAs(path);// easy async supportclient.ExecuteAsync(request, response => { Console.WriteLine(response.Content);});// async with deserializationvar asyncHandle = client.ExecuteAsync (request, response => { Console.WriteLine(response.Data.Name);});// abort the request on demandasyncHandle.Abort();