Extract GZip stream from HTTPClient response
Question:
How to decompress GZip encoded JSON response from API using WCF and HttpClient?
Answer:
To decompress a GZip encoded response using HttpClient:
<code class="language-csharp">HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };</code>
<code class="language-csharp">using (var client = new HttpClient(handler)) { // 您的代码 }</code>
Important Tips:
If using .Net Core 2.1 or higher, it is recommended to use IHttpClientFactory and inject the client with handler configuration. For example:
<code class="language-csharp">services.AddHttpClient<XApiClient>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });</code>
The above is the detailed content of How to Decompress GZip-Encoded JSON Responses from an HTTPClient in WCF?. For more information, please follow other related articles on the PHP Chinese website!