Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient vs HttpWebRequest

I have a large file which I have to send to a web api client...The data is multi part. The issue is , if the file is sent over http web request, then it is uploaded quickly on the webapi. For this request, file contents are written over the request stream directly.

Where as if the same file is sent over Httpclient (.net 4.5), the upload is slow when compared to http web request. I am using multipartformdatacontent in Httpclient post async.

So, for large files, do we have to use only web request? or is there any settings on Httpclient that makes the upload faster?

like image 922
user2325247 Avatar asked Mar 06 '14 04:03

user2325247


People also ask

What is the difference between HttpClient and HttpWebRequest?

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 .

Does HttpClient use HttpWebRequest?

HttpClient combines the flexibility of HttpWebRequest and WebClient. To start, we use the async and await keywords.

Is HttpWebRequest obsolete?

WebRequest, WebClient, and ServicePoint are obsolete.

Is RestSharp better than HttpClient?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.


1 Answers

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 which is taken from here

  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.
like image 65
Krimson Avatar answered Sep 25 '22 17:09

Krimson