Application Scenarios
It should be said that as long as it is necessary to send HTTP requests to obtain network resources in the place to use it, network resources can refer to a URI to represent the resources, such as the Web API interface.
HttpWebRequest
. net2.0 ~. net4.0 using HttpWebRequest
The code is as follows:
1 //.net2.0 ~. net4.0 using HttpWebRequest 2 var request = (HttpWebRequest) webrequest.create (requesturl);
3 var response = Request. GetResponse ();
4
using (StreamReader reader =
new StreamReader (response. GetResponseStream (), Encoding.UTF8))
5 {
6< /span> Responsejson = reader. ReadToEnd (); 7 } 8 9 //serialization var re Sult = responsejson.fromjson<tytrainstopresponse> ();
View CodeHttpClient
. NET 4.5+ using HttpClient
The code is as follows:
1//.net 4.5+ using HttpClient 2new HttpClient (); 3 Responsejson = Httpclient.getasync (Requesturl). Result.Content.ReadAsStringAsync (). Result; 4 5//Serialization 6 var result = responsejson.fromjson< Tytrainstopresponse> ();
View CodeDifference between the two
The most unusual thing about 1,httpclient is that the same httpclient instance can make multiple requests, and each request can be a completely different URL. And a HttpWebRequest instance corresponds to a request for a URL. This is the biggest difference between httpclient and HttpWebRequest.
2,httpclient is easier to use and less code.
Resources
Http://www.cnblogs.com/dudu/archive/2013/03/05/httpclient.html
How to send HTTP request network resources in. Net