background
In my usual job I occasionally write scripts to monitor the health of the HTTP interface, basically by sending an HTTP GET or HTTP POST request, and then detecting the response content. But it's always been WebClient and HttpWebRequest, and while they can be used for asynchronous requests (refer to the code I've shared: 3 Examples of C # asynchronous get), it's not always a natural thing to use. (For testing a large number of page links, StackOverflow developers have an article: Testing 3 million Hyperlinks, lessons learned)
An online search found that the. NET 4.5 introduced the HttpClient library to complete the asynchronous HTTP request. Then combined with multi-party data to summarize the characteristics and use of httpclient. This article belongs to the nature of reading notes blog.
preparatory work
Create a new console application in VS2012 (VS2010 should also be able), and then add a reference to the System.Net and System.Net.Http assemblies.
httpclient Introduction
HttpClient is an Http client library introduced by. NET4.5 that we might use WebClient and HttpWebRequest to achieve the same goal before the namespace is System.Net.Http. Net 4.5. But there are a few points to be concerned about: You can use a single httpclient instance to send any number of requests. A httpclient instance is not bound to an HTTP server or host. That is to say, we can use an instance to send requests to www.a.com and www.b.com at the same time to inherit httpclient to achieve custom purpose httpclient take advantage of the latest task-oriented mode, making it easy to process asynchronous requests
Asynchronous HTTP get
The following is an example of HTTP GET request data using httpclient:
View text print using system; using system.net.http; namespace httpclientproject { class HttpClientDemo { private const string Uri = "Http://api.worldbank.org/countries?format=json"; static void main (String[] args) { httpclient httpclient = new httpclient (); // Create an asynchronous get request to continue processing when the request returns httpclient.getasync (Uri). ContinueWith ( (RequestTask) = > { HttpResponseMessage response = requestTask.Result; // confirm the response is successful, otherwise throw exception response. Ensuresuccessstatuscode (); // asynchronous read response to string &Nbsp; response. Content.readasstringasync (). ContinueWith ( (Readtask) => console.writeline ( Readtask.result);   &NBSP}); console.writeline ("Hit enter to exit ..."); console.readline (); } } }
The
code will first output "Hit Enter to exit ..." before outputting the request response content because of the Getasync (string RequestUri) asynchronous method, which returns the task< Httpresponsemessage> Object ( Here's Httpclient.getasync (Uri). ContinueWith (...) Somewhat similar to the use Promise object for asynchronous programming in JavaScript, you can refer to the four methods of javascript asynchronous programming Section fourth and jquery of deferred objects). So we can rewrite the above code with the async, await keyword that is supported after. NET 4.5, and still have the asynchrony:
View text print using system; using system.net.http; namespace httpclientproject { class HttpClientDemo { private const string Uri = "Http://api.worldbank.org/countries?format=json"; static async void run () { Httpclient httpclient = new httpclient ();