这似乎是一个随机问题。有时 GetAsync 会成功获得响应 (200/OK)。其他时候它会抛出异常。我认为这与“传输编码:分块”有关。顺便说一句...流式文件的大小从 8MB 到 300MB
这是我的相关代码:
HttpResponseMessage response = null;
try
{
HttpClient httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(60)
};
httpClient.DefaultRequestHeaders.Add("Accept", "multipart/related; type=\"application/dicom\"");
response = await httpClient.GetAsync(uri);
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine($"Response:[{response}]");
return false;
}
}
catch (Exception e)
{
Console.WriteLine($"Exception:[{e.Message}] | Stack:[{e.StackTrace}] | InnerEx:[{e.InnerException}]");
return false;
}
这是 GetAsync 成功(200/OK)时的 HttpResponseMessage:
{
"Version": {
"Major": 1,
"Minor": 1,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [ "multipart/related; boundary=Boundary_77_676776408_1611644859244; type=\"application/dicom\"" ]
}
]
},
"StatusCode": 200,
"ReasonPhrase": "OK",
"Headers": [
{
"Key": "Transfer-Encoding",
"Value": [ "chunked" ]
},
{
"Key": "MIME-Version",
"Value": [ "1.0" ]
},
{
"Key": "Date",
"Value": [ "Tue, 26 Jan 2021 07:07:39 GMT" ]
},
{
"Key": "Proxy-Connection",
"Value": [ "Keep-Alive" ]
},
{
"Key": "Connection",
"Value": [ "Keep-Alive" ]
},
{
"Key": "Age",
"Value": [ "0" ]
}
],
"RequestMessage": {
"Version": {
"Major": 2,
"Minor": 0,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"Content": null,
"Method": { "Method": "GET" },
"RequestUri": "http://1.2.3.4:5007/blah/blah1/11/blah2/22/blah3/33",
"Headers": [
{
"Key": "Accept",
"Value": [ "multipart/related; type=\"application/dicom\"" ]
}
],
"Properties": {}
},
"IsSuccessStatusCode": true
}
其他时候没有响应并抛出异常:
异常:[服务器返回无效或无法识别的响应。] | 堆栈:[在 System.Net.Http.HttpConnection.ThrowInvalidHttpResponse() 在 System.Net.Http.HttpConnection.ChunkedEncodingReadStream.ReadChunkFromConnectionBuffer(Int32 maxBytesToRead, CancellationTokenRegistration cancelRegistration) 在 System.Net.Http.HttpConnection.ChunkedEncodingReadStream.CopyToAsyncCore(流目标, System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer) 在 System.Net.Http.HttpClient 的 System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer) 的 System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer) .FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage 请求, CancellationTokenSource cts, Boolean disposeCts) 在 myapp.GetBlahAsync(Uri uri, String newFileName) 在 C:\blah\Svc.cs:line 97] | 内壳:[]