php gzipstream,c# – 在WebRequest中发送gzip数据?

我有大量的数据(〜100k),我的C#应用​​程序发送到我的Apache服务器,安装了mod_gzip.我试图使用System.IO.Compression.GZipStream gzip数据.

PHP收到原始的gzip压缩数据,所以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...)

我不完全确定“Content-Encoding:gzip”适用于客户端提供的标题.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# ,可以使用 GZipStream 类轻松压缩字符串。以下是示例代码: ```csharp using System; using System.IO; using System.IO.Compression; class Program { static void Main(string[] args) { string originalString = "This is the original string to be compressed."; // 压缩字符串 byte[] compressedBytes; using (MemoryStream ms = new MemoryStream()) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress)) { using (StreamWriter writer = new StreamWriter(gzip)) { writer.Write(originalString); } } compressedBytes = ms.ToArray(); } // 将压缩后的字节数组转换回字符串 string compressedString = Convert.ToBase64String(compressedBytes); Console.WriteLine("Original string length: " + originalString.Length); Console.WriteLine("Compressed string length: " + compressedString.Length); } } ``` 在上面的代码,我们使用 GZipStream 类将原始字符串压缩为字节数组,然后将字节数组转换为 Base64 编码的字符串。可以看到,压缩后的字符串长度很短,这意味着我们可以使用更少的空间来存储数据。 如果需要解压缩字符串,可以使用类似以下的代码: ```csharp using System; using System.IO; using System.IO.Compression; class Program { static void Main(string[] args) { string compressedString = "H4sIAAAAAAAAALvJyCxWAKtLAwAAAA=="; // 将压缩后的字符串转换为字节数组 byte[] compressedBytes = Convert.FromBase64String(compressedString); // 解压缩字节数组并转换为原始字符串 string originalString; using (MemoryStream ms = new MemoryStream(compressedBytes)) { using (GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(gzip)) { originalString = reader.ReadToEnd(); } } } Console.WriteLine("Original string: " + originalString); } } ``` 在上面的代码,我们使用 GZipStream 类将压缩后的字节数组解压缩为原始字符串。需要注意的是,解压缩后的字符串可能包含不可见字符,因此建议使用 Base64 编码来转换压缩后的字节数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值