日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

服务器流式响应,HttpClient在收到服务器响应后无法停止流式传输

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 服务器流式响应,HttpClient在收到服务器响应后无法停止流式传输 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我一直在使用.NET 4.5中的HttpClient掙扎了一段時間。在通過分塊傳輸對WebApi端點進行大型流式上傳時,如果服務器已通過非成功狀態代碼(未找到,認證,授權,驗證錯誤等)響應中間請求,則無法停止。HttpClient在收到服務器響應后無法停止流式傳輸

使用ConnectStream類中的調試器查看它知道它從服務器收到了一個不成功的狀態。從服務器接收到的HTTP狀態和數據將被存儲以備后用,并且從那時起它只是假裝從流中讀取直到結束。從那時起,線路上沒有任何信息被寫入,并且后來通過HttpResponseMessage提供了響應。這種無法解釋的行為給我造成了巨大的問題,因為所涉及的流可能非常大?;ㄙM無用的時間讀取流直到結束,并向用戶顯示不正確的上載報告。我停止了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);

2015-12-16

MoonStom

+0

你能顯示你的代碼嗎? –

總結

以上是生活随笔為你收集整理的服务器流式响应,HttpClient在收到服务器响应后无法停止流式传输的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。