Aviram Fireberger Aviram Fireberger - 1 year ago 133
HTTP Question

Why .Net HttpClient post request is slow (when fiddler is not running)?

I'm getting this weird behavior using "HttpClient" .Net component.

I'm uploading a file (1.1MB) in a post request. When fiddler turn off it's takes about 15 sec when fiddler is on it's takes about 4 sec.

I'm not using any proxy, uploading to a HTTPS server using TLS1.
I send only one request, not sure the keep-alive changing anything...
I tried also to do some of the things "telerik" mentioned here:

But it didn't worked,

Is there another setting that I missed? Buffer size? not sure how to set it..

This is how I upload the file:

HttpClient _httpClient;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

MultipartFormDataContent multipartData = new MultipartFormDataContent();
multipartData.Add(new StreamContent(File.OpenRead(scanPath)), "fileToUpload","\"" + Path.GetFileName(scanPath) + "\"");

HttpResponseMessage response = await _httpClient.PostAsync("FileUpload", multipartData);

MyObject result = await GetResultFromResponse<MyObject>(response);

Answer Source

Solved by using "ByteArrayContent", instead of the "StreamContent".

In the "add" method of the "MultiPartFromData" class

This type of HttpContent is about 5X-8X faster.

multipartData.Add(new ByteArrayContent(File.ReadAllBytes(scanPath)), "fileToUpload", "\"" + Path.GetFileName(scanPath) + "\"");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download