HttpResponseMessage - 来自 API 调用的 Gzip 响应的解压抛出以下异常 - 响应过早结束

HttpResponseMessage - Decompression of Gzip respone from an API Call throws the following exceptiong - The response ended prematurely

我实际上正在使用 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();

异常信息也是"The response ended prematurely."

以下是堆栈跟踪:

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)

您是否尝试过使用 RestSharp 处理此请求? 它应该会为你搞定一切。

http://restsharp.org/

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

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

这应该让您的代码等待整个响应被读取。