正在解压缩 Web Api 响应

Decompressing Web Api response

我正在使用以下配置压缩网络 Api 响应

<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/*" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/*" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />

现在,当我在 Win Form 应用程序中使用它并尝试执行以下操作时

var rawData = await response.Content.ReadAsStringAsync();
                   var deserializedData = JsonConvert.DeserializeObject<Employees[]>(rawData).ToList();

它在

上失败

var deserializedData = JsonConvert.DeserializeObject(rawData).ToList();

错误信息是

{"Unexpected character encountered while parsing value: . Path '', line 0, position 0."}

我猜这是因为内容是 gzip 压缩的,没有被反序列化。谁能提出解决方案?这在本地工作正常,因为本地 IIS 未启用 gzip

您需要启用自动 GZip 解压缩:

var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
var client = new HttpClient(handler);