【问题标题】:Why .Net HttpClient post request is slow (when fiddler is not running)?为什么 .Net HttpClient 发布请求很慢(当提琴手没有运行时)?
【发布时间】:2016-11-22 15:40:08
【问题描述】:

我在使用“HttpClient”.Net 组件时遇到了这种奇怪的行为。

我正在发布请求中上传一个文件 (1.1MB)。 fiddler 关闭大约需要 15 秒,fiddler 开启大约需要 4 秒。

我没有使用任何代理,使用 TLS1 上传到 HTTPS 服务器。 我只发送一个请求,不确定keep-alive会改变什么...... 我还尝试做这里提到的“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);

【问题讨论】:

  • HttpClient无关,要么你的连接速度很慢,要么你的网络设置有问题。
  • 嗯,我的连接很好,使用相同的 PC 相同的应用程序运行提琴手我让它上传速度快了大约 350℅ - 持久。
  • 上传更大的文件时更糟。对于 10MB 的文件,我会在 fiddler 开启 25 秒后上传,大约 3 分钟后 fiddler 关闭。

标签: c# .net http httpclient fiddler


【解决方案1】:

通过使用“ByteArrayContent”而不是“StreamContent”来解决。

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

这种类型的 HttpContent 大约快 5X-8X。

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

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多