浏览器会自动解压缩GZIP吗? [英] Is GZIP Automatically Decompressed by Browser?

查看:1165
本文介绍了浏览器会自动解压缩GZIP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过遵循url在IIS 8.0中启用了gzip压缩 在Windows 8的IIS中启用Gzip 我正在通过jquery ajax调用和C#代码从应用程序中调用外部Rest服务,当前我的外部Web服务尚未gzip压缩.如果我要求我的服务合作伙伴对他们的响应进行gzip压缩,是否需要在我的jquery端和c#端的代码中编写任何解压缩逻辑,或者浏览器会自动为我解压缩响应?

I have enabled gzip compression in IIS 8.0 by following the url Enabling Gzip in IIS on Windows 8 I am calling external rest services from my application via jquery ajax call and C# code, currently my external web service is not gzip compressed. If i ask my service partner to gzip their response, do i need to write any decompression logic in my code on jquery side and c# side or browser automatically decompress the response for me ?

推荐答案

所有现代浏览器都可以处理gzip编码的响应.实际上,如果您查看他们的请求,他们将有一个标头,上面写着Accept-Encoding: gzip的内容,这是他们向服务器说他们可以处理压缩响应的方式.

All modern browsers can handle a gzip encoded response. In fact, if you look at their requests, they'll have a header that says something along the lines of Accept-Encoding: gzip which is their way of saying to the server that they can handle gzipped responses.

重要的是,您的服务器可以根据该标头的存在和值返回gzip和未压缩的响应.如果客户端不发送Accept-Encoding标头,则不应压缩它.如果客户端确实发送了它,则可以选择使用gzip对响应进行编码.并非所有内容都需要压缩,因为它可能已经被压缩了,您正在浪费CPU周期. JPEG图像通常是一个很好的例子. IIS很可能也在这里做出明智的决定,并且仅在必要时压缩必要的内容.

The important part is that your server can return both gzip and uncompressed responses depending on the existence and value of that header. If a client doesn't send the Accept-Encoding header, you shouldn't compress it. If the client does send it, you can optionally encode the response using gzip. Not all content needs to be compressed as it may already be compressed and you're wasting CPU cycles. JPEG images are usually a good example of this. Most likely, IIS is making an intelligent decision here as well and only compressing what is necessary, when necessary.

通过查看从服务器返回的响应标头并查找Content-Encoding: gzip标头,可以验证IIS是否在执行应做的工作.这样可以告诉客户端或浏览器,该内容是使用gzip压缩编码的,应该适当地对其进行解压缩.

You can verify IIS is doing what it should be by looking at the response headers coming back from your server and looking for the Content-Encoding: gzip header. That tells the client, or browser, that the content is encoded using gzip compression and it should decompress it appropriately.

所有基于浏览器的请求(例如XHR/AJAX/jQuery,常规请求)将自动解压缩,而无需您付出额外的努力.浏览器是负责确定它是否可以处理gzip的客户端,如果可以,将添加Accept-Encoding标头.您的JavaScript代码将在响应处理程序中收到其未压缩版本.

All browser based requests (e.g., XHR/AJAX/jQuery, regular requests) will automatically be decompressed without additional effort from you. The browser is the client responsible for determining if it can handle gzip and will add the Accept-Encoding header if it does. Your JavaScript code will receive the uncompressed version of it in your response handler.

TL; DR :通常将其打开是个好主意,您无需进行其他工作.

TL;DR: Turning it on is usually a good idea and you shouldn't need to do additional work.

这篇关于浏览器会自动解压缩GZIP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆