How to send HTTP request network resources in. Net

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.