为什么.Net HttpClient发布请求很慢(当fiddler没有运行时)?

时间:2016-11-22 15:40:08

标签: c# .net http httpclient fiddler

我使用“HttpClient”.Net组件获得了这种奇怪的行为。

我在帖子请求中上传了一个文件(1.1MB)。当小提琴手关闭时,当小提琴手打开时需要大约15秒钟,大约需要4秒。

我没有使用任何代理,使用TLS1上传到HTTPS服务器。 我只发送一个请求,不确定保持活动改变任何东西...... 我还试着做一些这里提到的“telerik”: http://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app-

但它没有用,

我错过了另一种设置吗?缓冲区大小?不知道如何设置..

这是我上传文件的方式:

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

1 个答案:

答案 0 :(得分:0)

使用“ByteArrayContent”解决,而不是“StreamContent”。

在“MultiPartFromData”类的“add”方法中

这种类型的HttpContent的速度提高了约5倍-8倍。

multipartData.Add(new ByteArrayContent(File.ReadAllBytes(scanPath)), "fileToUpload", "\"" + Path.GetFileName(scanPath) + "\"");