C#HttpWebRequest的 - 使用gzip的COM pressiongzip、HttpWebRequest、pression、COM

2023-09-07 08:51:54 作者:23.幼稚园萌小疯

我有一个计划,使得很多HttpWebRequests的,和我读到有关使用gzip COM pression加快响应数据的下载。我知道gzip的是什么,以及它是如何工作的,但我不知道它是如何工作在C#。

I have a program which makes lots of HttpWebRequests, and I read about using gzip compression to speed up the downloading of the response data. I know what gzip is, and how it works, but I don't know how it works in C#.

让我们说我有一个简单的GET请求:

Let's say I have a simple GET request:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
request.Method = "GET";
WebResponse response = request.GetResponse();

我怎么能做出响应数据进行COM pressed采用gzip?我怎样才能显示COM pressed大小,则未COM pressed大小?

How could I make the response data be compressed in gzip? How can I show the compressed size, and then the un-compressed size?

感谢

推荐答案

请确保你在你的要求,你能接受gzip压缩响应状态。所以,在你的code创建的要求,做到这一点:

Make sure you state in your request that you can accept gzip'd responses. So, after your code to create the request, do this:

request.Headers.Add("Accept-Encoding", "gzip");

这将告诉服务器返回它压缩,如果可以。

This will tell the server to return it zipped, if it can.

我觉得展会的规模差异,唯一的办法就是为t使2电话,其中一个COM pression,一个没有了,比较的返回响应流的长度。

The only way I can think to show the size difference would be t make 2 calls, one with compression and one not, and compare the length of the returned response streams.