服务器流式响应,HttpClient在收到服务器响应后无法停止流式传输
我一直在使用.NET 4.5中的HttpClient掙扎了一段時間。在通過分塊傳輸對WebApi端點(diǎn)進(jìn)行大型流式上傳時,如果服務(wù)器已通過非成功狀態(tài)代碼(未找到,認(rèn)證,授權(quán),驗(yàn)證錯誤等)響應(yīng)中間請求,則無法停止。HttpClient在收到服務(wù)器響應(yīng)后無法停止流式傳輸
使用ConnectStream類中的調(diào)試器查看它知道它從服務(wù)器收到了一個不成功的狀態(tài)。從服務(wù)器接收到的HTTP狀態(tài)和數(shù)據(jù)將被存儲以備后用,并且從那時起它只是假裝從流中讀取直到結(jié)束。從那時起,線路上沒有任何信息被寫入,并且后來通過HttpResponseMessage提供了響應(yīng)。這種無法解釋的行為給我造成了巨大的問題,因?yàn)樗婕暗牧骺赡芊浅4蟆;ㄙM(fèi)無用的時間讀取流直到結(jié)束,并向用戶顯示不正確的上載報(bào)告。我停止了Web服務(wù)器,遠(yuǎn)程機(jī)器,甚至禁用了本地網(wǎng)絡(luò)接口。 HttpClient不在乎。它不斷從提供的流中讀取數(shù)據(jù)。
我試過HttpCompletionOption.ResponseHeadersRead和StreamedContent或PushStreamContent。同樣的行為。我也嘗試過使用HttpWebRequest,但是它不允許在輸出流上寫入并同時讀取傳入流。有什么辦法阻止客戶端在收到服務(wù)器響應(yīng)后發(fā)送或假裝發(fā)送流?為什么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);
2015-12-16
MoonStom
+0
你能顯示你的代碼嗎? –
總結(jié)
以上是生活随笔為你收集整理的服务器流式响应,HttpClient在收到服务器响应后无法停止流式传输的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【暴力破解】medusacrowbar工
- 下一篇: 应学会推销自己