This seems to be a random issue. Sometimes the GetAsync successfully gets a response (200/OK). Other times it throws an exception.
I'm thinking it has something to do with the "Transfer-Encoding: chunked".
BTW...the streamed files are anywhere from 8MB to 300MB
Here is my pertinent code:
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;
}
Here's the HttpResponseMessage when GetAsync is successful (200/OK):
{
"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
}
Other times there is no response and an exception is thrown:
Exception:[The server returned an invalid or unrecognized response.] | Stack:[ at System.Net.Http.HttpConnection.ThrowInvalidHttpResponse()
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.ReadChunkFromConnectionBuffer(Int32 maxBytesToRead, CancellationTokenRegistration cancellationRegistration)
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.CopyToAsyncCore(Stream destination, CancellationToken cancellationToken)
at System.Net.Http.HttpConnection.HttpConnectionResponseContent.SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at myapp.GetBlahAsync(Uri uri, String newFileName) in C:lahSvc.cs:line 97] | InnerEx:[]
question from:
https://stackoverflow.com/questions/65904979/c-sharp-httpclient-getasync-exception-the-server-returned-an-invalid-or-unrecog