Keep it Simple

// practice, the distance between dream and achievement

WebRequest vs HttpWebRequest vs WebClient vs HttpClient

The .NET framework offers you three different classes to consume REST APIs: HttpWebRequest, WebClient, HttpClient. To worsen your analysis paralysis the open-source community created yet another library called RestSharp. Fear not, I’ll ease your choice.

Before diving into details, here is quick summary

  • System.Net.WebRequest is an abstract class
  • System.Net.HttpWebRequest and System.Net.FileWebRequest will inherit the WebRequest.
  • System.Net.WebClient higher-level abstraction built on top of HttpWebRequest.
  • System.Net.Http.HttpClient is available from 4.5
  • System.Web.HttpRequest class enables ASP.NET to read the HTTP values sent by a client during a Web request.
  • System.Web.HttpResponse class encapsulates HTTP-response information from an ASP.NET operation.

 

WebRequest
This is an abstract class, either you may have to create a HttpWebRequest or FileWebRequest to work with.

 

HttpWebRequest
This is the standard class that the .NET creators originally developed to consume HTTP requests. Using HttpWebRequest gives you control over every aspect of the request/response object, like timeouts, cookies, headers, protocols.

Another great thing is that HttpWebRequest class does not block the user interface thread. For instance, while you’re downloading a big file from a sluggish API server, your application’s UI will remain responsive.

However, with great power comes great complexity.

In order to make a simple GET you need at least five lines of code; we’ll see WebClient does it in two.

 

The number of ways you can make a mistake with HttpWebRequest is truly astounding. Only use HttpWebRequest if you require the additional low-level control that it offers.

 

WebClient
WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks. Using WebClient is potentially slower (on the order of a few milliseconds) than using HttpWebRequest directly. But that “inefficiency” comes with huge benefits: it requires less code, is easier to use, and you’re less likely to make a mistake when using it. That same request example is now as simple as

 

HttpClient
Http­Client is more closer to HTTP than Web­Client. Http­Client was not meant to be a com­plete replace­ment of Web Client, since there are things like report progress, cus­tom URI scheme and mak­ing FTP calls that Web­Client pro­vides — but Http­Client doesn’t.

If you’re using .NET 4.5, please do use the async good­ness with Http­Client that Microsoft pro­vides to the devel­op­ers. Http­Client is very sym­met­ri­cal to the server side brethren of the HTTP those are HttpRe­quest and HttpResponse. Refer this Link for further details.

httpclient

HttpRequest & HttpResponse
The HttpRequest class is under System.Web namespace. It contains properties and methods necessary to handle an HTTP request. It contains all information passed by the browser, including all form variables, certificates and header information. It also contains the CGI server variables.

The HttpResponse class is under System.Web namespace. It contains methods and properties necessary to crafting an HTTP response. Several methods are used to modify headers, cookies, and the actual content of the response.

 

There are few ways to Post JSON request

or

1 Comment

  1. excellent post , very nice article…

Leave a Reply

Your email address will not be published.

*

© 2017 Keep it Simple

Theme by Anders NorenUp ↑