简体   繁体   中英

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: http://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app-

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);

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) + "\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM