Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between HttpResponseMessage and HttpWebResponse?

They both seem to be different ways of handling responses to the client.

More detail about my problem: I have a server in which when I receive a request from a client I want to call a second server and return the response from that second server to my client.

like image 883
Zain Rizvi Avatar asked Jan 30 '14 00:01

Zain Rizvi


People also ask

What is the difference between IHttpActionResult and HttpResponseMessage?

IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance. If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.

What is HttpResponseMessage?

A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words an HttpResponseMessage is a way of returning a message/data from your action.

What is the difference between WebRequest and HttpClient?

In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .

Should HttpResponseMessage be disposed?

The safest, general advice would be to always dispose of the HttpResponseMessage once you have finished with using it. This does lead to a little more code noise but ensures that regardless of the internals and any future changes, your code will free/clean up unused resources such as connections as quickly as possible.


2 Answers

They both serve the same purpose.

  • HttpWebRequest/HttpWebResponse are available since the first version of .NET, and are still a perfectly valid approach.
  • HttpClient (which uses HttpRequestMessage and HttpResponseMessage to represent requests and responses) has been introduced in .NET 4.5, and offers a fully asynchronous API, along with a new model for request and response content; internally, it still relies on HttpWebRequest/HttpWebResponse.

An important difference is that HttpWebRequest/Response represent the request and response from a client point of view only, whereas HttpRequestMessage/HttpResponseMessage can be use by either a client or a server (ASP.NET Web API uses these types to communicate with the client).

You can use the one you're most comfortable with; just be aware that since HttpClient is asynchronous, the code that uses it must be asynchronous as well.

like image 180
Thomas Levesque Avatar answered Oct 17 '22 03:10

Thomas Levesque


HttpResponseMessage represent a moden way to use Http. It is being used by REST solutions such as Web API to manipulate status code and the Location header.

HttpWebResponse is goold old class that contains all the Http information but considers Obsolete.

like image 31
Amir Katz Avatar answered Oct 17 '22 04:10

Amir Katz