HttpClient vs HttpWebRequest RRS feed

  • Question

  • When using HttpWebRequest, I can get the access to request stream via HttpWebRequest.GetRequestStream and use it directly to write data. How do I achieve this with HttpClient?
    Thursday, September 18, 2014 8:14 PM


  • Hi Alex V. lvanoff,

    For the difference between the HttpClient and HttpWebRequest, please try to check the following information which comes from here.

    HttpClient is more like a head-less browser. It a powerfull and ideal tool if you are going to be creating many http request. For example you can set default headers and stuff. Here are the top 5 ways it differs from an HttpWebRequest:

    1. An HttpClient instance is the place to configure extensions, set default headers, cancel outstanding requests and more.
    2. You can issue as many requests as you like through a single HttpClient instance.
    3. HttpClients are not tied to particular HTTP server or host; you can submit any HTTP request using the same HttpClient instance.
    4. You can derive from HttpClient to create specialized clients for particular sites or patterns
    5. HttpClient uses the new Task-oriented pattern for handling asynchronous requests making it dramatically easier to manage and coordinate multiple outstanding requests.

    From more information, please check here: http://blogs.msdn.com/b/henrikn/archive/2012/02/11/httpclient-is-here.aspx .

    We often use the HttpClient is this way:

    HttpClient client = new HttpClient();
    var post = client.PostAsync<string>("http://localhost:58072/ResearchAPI.aspx/push", 
        "input1=abc&input2=cde", new XmlMediaTypeFormatter()).Result;
    string result = post.Content.ReadAsStringAsync().Result;
    For more information, please try to refer to:
    http://sylvester-lee.blogspot.jp/2013/04/httpclient-httpwebrequest-confusion.html .

    Best Regards,
    Amy Peng

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 19, 2014 2:21 AM