C# 在 WebRequest 中发送压缩数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1183691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:16:07  来源:igfitidea点击:

Sending gzipped data in WebRequest?

c#gzipwebrequestgzipstream

提问by

I have a large amount of data (~100k) that my C# app is sending to my Apache server with mod_gzip installed. I'm attempting to gzip the data first using System.IO.Compression.GZipStream. PHP receives the raw gzipped data, so Apache is not uncompressing it as I would expect. Am I missing something?

我有大量数据(~100k),我的 C# 应用程序将这些数据发送到安装了 mod_gzip 的 Apache 服务器。我试图首先使用 System.IO.Compression.GZipStream 对数据进行 gzip。PHP 接收原始 gzipped 数据,因此 Apache 并没有像我期望的那样解压缩它。我错过了什么吗?

System.Net.WebRequest req = WebRequest.Create(this.Url);
req.Method = this.Method; // "post"
req.Timeout = this.Timeout;
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("Content-Encoding: gzip");

System.IO.Stream reqStream = req.GetRequestStream();

GZipStream gz = new GZipStream(reqStream, CompressionMode.Compress);

System.IO.StreamWriter sw = new System.IO.StreamWriter(gz, Encoding.ASCII);
sw.Write( large_amount_of_data );
sw.Close();

gz.Close();
reqStream.Close()


System.Net.WebResponse resp = req.GetResponse();
// (handle response...)

I'm not entirely sure "Content-Encoding: gzip" applies to client-supplied headers.

我不完全确定“内容编码:gzip”是否适用于客户端提供的标头。

回答by Sean A.O. Harney

According to http://www.dominoexperts.com/articles/GZip-servlet-to-gzip-your-pages

根据http://www.dominoexperts.com/articles/GZip-servlet-to-gzip-your-pages

You should setContentType() to the original format, as you are doing with the application/x-www-form-urlencoded I assume. Then...

您应该将ContentType() 设置为原始格式,就像我假设的 application/x-www-form-urlencoded 一样。然后...

 // See if browser can handle gzip
 String encoding=req.getHeader("Accept-Encoding");
 if (encoding != null && encoding.indexOf("gzip") >=0 ) {  // gzip browser 
      res.setHeader("Content-Encoding","gzip");
      OutputStream o=res.getOutputStream();
      GZIPOutputStream gz=new GZIPOutputStream(o);
      gz.write(content.getBytes());
      gz.close();
      o.close();
            } else {  // Some old browser -> give them plain text.                        PrintWriter o = res.getWriter();
                    o.println(content);
                    o.flush();
                    o.close();
            }

回答by Martin Liversage

I looked at the source code for mod_gzipand I could not find any code that decompressesdata. Apparently mod_gziponly compresses outgoing data which isn't too surprising after all. The functionality you are looking for is probably rarely used, and I'm afraid you have to do your own decompression on the server.

我查看了源代码,mod_gzip但找不到任何解压缩数据的代码。显然mod_gzip只压缩传出数据,毕竟这并不奇怪。你要找的功能可能很少用到,恐怕你得自己在服务器上解压。

回答by Sergii Volchkov

Regarding your question whether Content-Encoding is applicable to client-supplied headers - according to HTTP/1.1 standard, it is:

关于您的问题 Content-Encoding 是否适用于客户端提供的标头 - 根据HTTP/1.1 标准,它是:

(from section 7)

(来自第 7 节)

Request and Response messages MAY transfer an entity if not otherwise restricted by the request method or response status code.

如果请求方法或响应状态代码不受其他限制,则请求和响应消息可以传输实体。

(from section 7.1)

(来自第 7.1 节)

   entity-header  = Allow                    ; Section 14.7
                  | Content-Encoding         ; Section 14.11
                  | Content-Language         ; Section 14.12
                  | Content-Length           ; Section 14.13
                  | Content-Location         ; Section 14.14
                  | Content-MD5              ; Section 14.15
                  | Content-Range            ; Section 14.16
                  | Content-Type             ; Section 14.17
                  | Expires                  ; Section 14.21
                  | Last-Modified            ; Section 14.29
                  | extension-header
   entity-header  = Allow                    ; Section 14.7
                  | Content-Encoding         ; Section 14.11
                  | Content-Language         ; Section 14.12
                  | Content-Length           ; Section 14.13
                  | Content-Location         ; Section 14.14
                  | Content-MD5              ; Section 14.15
                  | Content-Range            ; Section 14.16
                  | Content-Type             ; Section 14.17
                  | Expires                  ; Section 14.21
                  | Last-Modified            ; Section 14.29
                  | extension-header

回答by nos

You need to change

你需要改变

req.Headers.Add("Content-Encoding: gzip");

to

req.Headers.Add("Content-Encoding","gzip");

回答by addr010

On the PHP side this will strip out header and footer from file

在 PHP 方面,这将从文件中去除页眉和页脚

function gzip_stream_uncompress($data) { return gzinflate(substr($data, 10, -8)); }

function gzip_stream_uncompress($data) { return gzinflate(substr($data, 10, -8)); }