我有一个WebAPI,我在WebApiConfig中配置压缩:
config.MessageHandlers.Add(new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
在我的winforms客户端中,我使用像这样的HttpClient:
WebRequestHandler handler = new WebRequestHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
};
client = HttpClientFactory.Create(new ClientCompressionHandler(handler, new GZipCompressor(), new DeflateCompressor()));
client.BaseAddress = new Uri(serverUrl);
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
服务器和客户端项目都引用: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/
当我向api发出请求时,无论是否使用压缩,内容长度始终相同。所以我不知道内容是否被压缩。
特定于压缩的IIS applicationhost.config如下所示:
<httpCompression directory="%TEMP%\iisexpress\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%IIS_BIN%\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/xml; charset=utf-8" enabled="true" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
我检查了%TEMP%文件夹,并且没有名为iisexpress的子文件夹,所以我不得不假设使用上面的配置没有启用压缩。
有没有办法检查内容是否在被反序列化之前被压缩了?
如果使用上面的代码,api会压缩响应吗?或者它是IIS Express中的设置吗?