解压缩Web Api响应

时间:2015-07-13 13:18:45

标签: winforms asp.net-web-api gzip

我正在使用以下配置压缩Web 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."}

我想这是因为内容是gziped并且没有被反序列化。谁有人建议解决方案?这在本地工作正常,因为本地IIS不支持gzip

1 个答案:

答案 0 :(得分:1)

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

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