Reputation: 61
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:\blah\Svc.cs:line 97] | InnerEx:[]
Upvotes: 2
Views: 1262
Reputation: 834
is because you need deserialize response
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.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var result = JsonSerializer.Deserialize<Root>(responseData, options);
Console.WriteLine($"Response:[{result }]");
return result;
}
}
catch (Exception e)
{
Console.WriteLine($"Exception:[{e.Message}] | Stack:[{e.StackTrace}] | InnerEx:[{e.InnerException}]");
return false;
}
for convert your json to c# class use this site Json to c# class
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Version {
public int Major { get; set; }
public int Minor { get; set; }
public int Build { get; set; }
public int Revision { get; set; }
public int MajorRevision { get; set; }
public int MinorRevision { get; set; }
}
public class Header {
public string Key { get; set; }
public List<string> Value { get; set; }
}
public class Content {
public List<Header> Headers { get; set; }
}
public class Method {
public string Method { get; set; }
}
public class Properties {
}
public class RequestMessage {
public Version Version { get; set; }
public object Content { get; set; }
public Method Method { get; set; }
public string RequestUri { get; set; }
public List<Header> Headers { get; set; }
public Properties Properties { get; set; }
}
public class Root {
public Version Version { get; set; }
public Content Content { get; set; }
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public List<Header> Headers { get; set; }
public RequestMessage RequestMessage { get; set; }
public bool IsSuccessStatusCode { get; set; }
}
or in visual studio in Edit-->Paste Special-->Paste JSON As Classes
Upvotes: 0