Original link: http://www.cnblogs.com/TianFang/p/3724449.html
WEBAPI is a standard HTTP protocol that can be accessed by clients that support the HTTP protocol, such as a browser. However, sometimes when we want to use WEBAPI in our own programs, we have to implement our own client at this time. I've previously introduced the new HttpClient library in. Net 4.5, which provides a very good encapsulation of HTTP operations. We can implement HTTP access through it, for example, we post the API as shown earlier:
Post:api/values
public void Post (Product value)
{
}
Start with some initialization of httpclient:
var client = new HttpClient ();
Client. baseaddress = new Uri ("http://localhost:1282/");
Client. DefaultRequestHeaders.Accept.Clear ();
Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/json"));
Here are two main operations: 1. The default base address is defined, reducing the subsequent URL length, 2. Defines the default accepted data type as JSON.
The next step is to start the content encoding of the Product object, which, by default, is XML or JSON, where I choose a relatively simple json:
var product = new Product () {Id = 1, Name = "Food"};
var content = Newtonsoft.Json.JsonConvert.SerializeObject (product);
var httpcontent = new Stringcontent (content, Encoding.UTF8);
HttpContent.Headers.ContentType = new Mediatypeheadervalue ("Application/json") {CharSet = "utf-8"};
await Httpcontent.loadintobufferasync ();
var RSP = await client. Postasync ("Api/values", httpcontent);
As can be seen from the above code, because WEBAPI does not like WCF can automatically generate client code, we need to encapsulate the object, the light encapsulated object on the use of four lines of code, for unfamiliar with httpclientd friends is still more error-prone. So Microsoft offers a range of extension functions that allow us to simplify this process.
Installing the Webapi.client library in NuGet
This library will add a System.Net.Http.Formatting assembly to the reference after installation, it mainly provides a series of extension functions (in fact, this library will also put httpclient and json.net ashore), now the above code can be simplified as follows:
var product = new Product () {Id = 1, Name = "Food"};
var RSP = await client. Postasjsonasync ("Api/values", product);
In addition to postasjsonasync this extension function, it also provides a way to transmit postasxmlasync in XML. Similarly, put also has an extended version of Putasjsonasync and Putasxmlasync. For get, although extension functions are provided, they are used in slightly different ways:
var RSP = await client. Getasync ("API/VALUES/1");
Rsp. Ensuresuccessstatuscode ();
var product = await RSP. Content.readasasync<product> ();
For delete, there is no extension function, it may be official that the delete directly in the URL to pass the ID is enough, there is no need to encapsulate the message in the request.
Building a rest service with ASP. WEBAPI-Client