Home > Backend Development > C++ > How to Decompress GZip-Encoded JSON Responses from an HTTPClient in WCF?

How to Decompress GZip-Encoded JSON Responses from an HTTPClient in WCF?

Patricia Arquette
Release: 2025-01-17 17:46:09
Original
346 people have browsed it

How to Decompress GZip-Encoded JSON Responses from an HTTPClient in WCF?

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:

  1. Enable decompression function and instantiate HttpClientHandler:
<code class="language-csharp">HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};</code>
Copy after login
  1. Use handler to create an HttpClient instance:
<code class="language-csharp">using (var client = new HttpClient(handler))
{
    // 您的代码
}</code>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template