我使用“HttpClient”.Net 组件遇到了这种奇怪的行为。
我正在发布请求中上传一个文件 (1.1MB)。当 fiddler 关闭时,大约需要 15 秒;当 fiddler 打开时,大约需要 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);
通过使用“ByteArrayContent”而不是“StreamContent”解决。
在“MultiPartFromData”类的“add”方法中
这种类型的 HttpContent 速度大约快 5 倍到 8 倍。
multipartData.Add(new ByteArrayContent(File.ReadAllBytes(scanPath)), "fileToUpload", "\"" + Path.GetFileName(scanPath) + "\"");