HttpResponseMessage-从API调用中解压缩Gzip会引发以下异常g-响应过早结束

时间:2019-12-16 14:52:16

标签: c# api gzip streamreader gzipstream

我实际上正在使用REST服务,从该服务我收到压缩为Gzip的响应 因此,以下是我的代码

HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var result = await httpClient.GetAsync(url);

我得到了最后一行“ await httpClient.GetAsync(url)”的异常

以下是堆栈跟踪:

at System.Net.Http.HttpConnection.<FillAsync>d__87.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.<CopyToAsyncCore>d__10.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
at System.IO.Compression.DeflateStream.CopyToStream.<CopyFromSourceToDestinationAsync>d__6.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.DecompressionHandler.DecompressedContent.<SerializeToStreamAsync>d__5.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__54.MoveNext()

作为快速更新:我确实尝试了以下不同的呼叫实现:

HttpWebRequest Http = (HttpWebRequest)WebRequest.Create("url");

Http.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
HttpWebResponse WebResponse = (HttpWebResponse)Http.GetResponse();
Stream responseStream = responseStream = WebResponse.GetResponseStream();
if (WebResponse.ContentEncoding.ToLower().Contains("gzip"))
{
    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
StreamReader Reader = new StreamReader(responseStream, Encoding.Default);
string responseAsString= Reader.ReadToEnd();
WebResponse.Close();
responseStream.Close();

我在下一行遇到异常     字符串responseAsString = Reader.ReadToEnd();

异常消息也为“响应过早结束”。

以下是堆栈跟踪:

at System.Net.Http.HttpConnection.Fill()
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.Read(Span`1 buffer)
at System.Net.Http.HttpBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Compression.DeflateStream.ReadCore(Span`1 buffer)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadToEnd()
at ApiCall.Program.<Main>d__0.MoveNext() in 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ApiCall.Program.<Main>(String[] args)

2 个答案:

答案 0 :(得分:1)

尝试将HttpCompletionOption.ResponseContentRead添加到您的GetAsync调用中:

var result = await httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead);

这应该使您的代码等待,直到读取完整个响应为止。

答案 1 :(得分:0)

您是否尝试过使用RestSharp进行此请求? 它应该为您做好一切。

http://restsharp.org/