c# – HttpClient在收到服务器的响应后无法停止流式传输
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – HttpClient在收到服务器的响应后无法停止流式传输,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3380字,纯文字阅读大概需要5分钟。
内容图文
![c# – HttpClient在收到服务器的响应后无法停止流式传输](/upload/InfoBanner/zyjiaocheng/773/2fbaadac1dba4abb85d16c390b4ac711.jpg)
我一直在使用.NET 4.5中的HttpClient进行一段时间的努力.通过分段传输大量流式传输到WebApi端点,当服务器已经使用非成功状态代码(未找到,身份验证,授权,验证错误等)响应中间请求时,它无法停止.
查看ConnectStream类中的调试器,它知道它从服务器收到了一个不成功的状态.从服务器接收的HTTP状态和数据将被存储以供稍后使用,从那时起它就只是假装从流中读取直到结束.从那一刻开始,没有任何东西写在电线上,之后通过HttpResponseMessage提供响应.这种无法解释的行为给我带来了很大的问题,因为有问题的流可能非常大.读取流到结束时花费了无用的时间,并且正在向用户显示不正确的上载报告.我停止了Web服务器,远程机器,甚至禁用了本地网络接口. HttpClient并不关心.它继续从提供的流中读取.
我尝试了HttpCompletionOption.ResponseHeadersRead和StreamedContent或PushStreamContent.相同的行为.我也尝试过使用HttpWebRequest,但是不允许在输出流上写入并同时读取传入的流.是否有任何方法可以阻止客户端在收到服务器的响应后发送或假装发送流?为什么HttpClient表现得像这样?
var httpClientHandler = new WebRequestHandler
{
AllowAutoRedirect = true,
PreAuthenticate = false,
CookieContainer = cookieContainer,
UseCookies = true,
CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore),
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
};
var httpClient = new HttpClient(_httpClientHandler)
{
BaseAddress = baseApiUrl,
Timeout = Timeout.InfiniteTimeSpan;
};
httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain", 0.8));
httpClient.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("UTF-8"));
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(appName, appVersion));
var requestContent = new StringContent(serializedRequest, Encoding.UTF8, "application/json");
// the monitored stream is an implementation of the stream that proxies the calls to a classic file stream
// here I monitor the calls made to Read(byte[] buffer, int offset, int count) method
// tried with both CanSeek = false or true - the HttpClient reads the whole stream no matter what.
var streamedContent = new StreamContent(monitoredStream);
streamedContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = "upload.bin",
Size = monitoredStream.Length
};
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, relativeUrl)
{
Content = new MultipartFormDataContent
{
requestContent,
streamedContent
}
};
// the Owin middleware could fail checking authentication, an authorization error could occur,
// or the WebApi controller could receive the first part of the multipart data and reject the request
// from the moment data was received from the server, mid-stream, I could safely unplug the network cable
// socket erros are being ignored, actually the underlying socket is no longer used
// the response message contains the status code and data received during the transmission of the request at the end of this call
var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
解决方法:
这是HttpClient内部确认的限制. Here is the Microsoft Connect case.也许它会在下一个版本中实现,但它不会影响它的投票.
内容总结
以上是互联网集市为您收集整理的c# – HttpClient在收到服务器的响应后无法停止流式传输全部内容,希望文章能够帮你解决c# – HttpClient在收到服务器的响应后无法停止流式传输所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。