Is GZIP Automatically Decompressed by Browser?

依然范特西╮ 提交于 2020-03-18 10:28:17

问题


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 ?


回答1:


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.

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.

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.

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: Turning it on is usually a good idea and you shouldn't need to do additional work.




回答2:


If the gzip compression is enabled on the web server, that is, not in the application logic, then the browser will uncompress automatically.

In fact, if the browser doesn't support compression the web server will send the data uncompressed (this info is in the request/response http headers exchanged between browser and web server). Just be aware that compression is ineffective with JPEG and other already compressed formats.



来源:https://stackoverflow.com/questions/32172704/is-gzip-automatically-decompressed-by-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!