如何压缩 HttpWebRequest POST

发布于 2024-10-02 13:26:35 字数 1039 浏览 0 评论 0原文

我正在尝试将数据发布到接受压缩数据的服务器。下面的代码工作得很好,但它是未压缩的。我以前没有使用过压缩或 Gzip,因此需要任何帮助。

HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
  request.Timeout = 600000;
  request.Method = verb;  // POST    
  request.Accept = "text/xml";

  if (!string.IsNullOrEmpty(data))
  {
    request.ContentType = "text/xml";        

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
    request.ContentLength = byteData.Length;       

    // Here is where I need to compress the above byte array using GZipStream

    using (Stream postStream = request.GetRequestStream())
    {
      postStream.Write(byteData, 0, byteData.Length);         
    }
  }      

  XmlDocument xmlDoc = new XmlDocument();
  HttpWebResponse response = null;
  StreamReader reader = null;
  try
  {
    response = request.GetResponse() as HttpWebResponse;
    reader = new StreamReader(response.GetResponseStream());
    xmlDoc.LoadXml(reader.ReadToEnd());
  }

我是否对整个字节数组进行 gzip 压缩?我是否需要添加其他标头或删除已有的标头?

谢谢!

-斯科特

I am trying to post data to server that accepts compressed data. The code below works just fine, but it is uncompressed. I have not worked with compression or Gzip beofre, so any help is appriciated.

HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
  request.Timeout = 600000;
  request.Method = verb;  // POST    
  request.Accept = "text/xml";

  if (!string.IsNullOrEmpty(data))
  {
    request.ContentType = "text/xml";        

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
    request.ContentLength = byteData.Length;       

    // Here is where I need to compress the above byte array using GZipStream

    using (Stream postStream = request.GetRequestStream())
    {
      postStream.Write(byteData, 0, byteData.Length);         
    }
  }      

  XmlDocument xmlDoc = new XmlDocument();
  HttpWebResponse response = null;
  StreamReader reader = null;
  try
  {
    response = request.GetResponse() as HttpWebResponse;
    reader = new StreamReader(response.GetResponseStream());
    xmlDoc.LoadXml(reader.ReadToEnd());
  }

Do I gzip the entire byte array? Do I need to add other headers or remove the one that is already there?

Thanks!

-Scott

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Hello爱情风 2024-10-09 13:26:35

要回答您提出的问题,要发布压缩数据,您所需要做的就是用 gzip 流包装请求流,

 using (Stream postStream = request.GetRequestStream())
 {
    using(var zipStream = new GZipStream(postStream, CompressionMode.Compress))
    {
        zipStream.Write(byteData, 0, byteData.Length);         
    }
 }

这与请求 gzip 响应完全不同,后者是更常见的事情。

To answer the question you asked, to POST compressed data, all you need to do is wrap the request stream with a gzip stream

 using (Stream postStream = request.GetRequestStream())
 {
    using(var zipStream = new GZipStream(postStream, CompressionMode.Compress))
    {
        zipStream.Write(byteData, 0, byteData.Length);         
    }
 }

This is completely different than requesting a gzip response, which is a much more common thing to do.

神经大条 2024-10-09 13:26:35

我还使用类似于 tnyfst 的代码收到了“在写入所有字节之前无法关闭流”错误。问题是我有:

request.ContentLength = binData.Length;

binData 是压缩前的原始数据。显然,压缩内容的长度会有所不同,因此我删除了这一行并最终得到以下代码:

using (GZipStream zipStream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress))
{
    zipStream.Write(binData, 0, binData.Length);
}

I also received the "Cannot close stream until all bytes are written" error using code similar to tnyfst's. The problem was that I had:

request.ContentLength = binData.Length;

where binData is my raw data before the compression. Obviously the length of the compressed content would be different, so I just removed this line and ended up with this code:

using (GZipStream zipStream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress))
{
    zipStream.Write(binData, 0, binData.Length);
}
难得心□动 2024-10-09 13:26:35

Page_Load 事件中:

        Response.AddHeader("Content-Encoding", "gzip");

对于发出压缩请求:

HttpWebRequest 和GZip Http 响应 作者:Rick Strahl

In Page_Load event:

        Response.AddHeader("Content-Encoding", "gzip");

And for making compressed requests:

HttpWebRequest and GZip Http Responses by Rick Strahl

浅暮の光 2024-10-09 13:26:35

尝试一下这个扩展方法。
该流将保持打开状态(请参阅 GZipStream 构造函数)。
压缩完成后,流位置设置为 0。

public static void GZip(this Stream stream, byte[] data)
{
    using (var zipStream = new GZipStream(stream, CompressionMode.Compress, true))
    {
        zipStream.Write(data, 0, data.Length);
    }
    stream.Position = 0;
}

您可以使用以下测试:

[Test]
public void Test_gzip_data_is_restored_to_the_original_value()
{
    var stream = new MemoryStream();
    var data = new byte[]{1,2,3,4,5,6,7,8,9,10};

    stream.GZip(data);

    var decompressed = new GZipStream(stream, CompressionMode.Decompress);

    var data2 = new byte[10];
    decompressed.Read(data2,0,10);

    Assert.That(data, Is.EqualTo(data2));
}

有关详细信息,请参阅: http://msdn.microsoft.com/en-us/library/hh158301(v=vs.110).aspx

Try this extension method.
The stream will be left open (see the GZipStream constructor).
The stream position is set to 0 after the compression is done.

public static void GZip(this Stream stream, byte[] data)
{
    using (var zipStream = new GZipStream(stream, CompressionMode.Compress, true))
    {
        zipStream.Write(data, 0, data.Length);
    }
    stream.Position = 0;
}

You can use the following test:

[Test]
public void Test_gzip_data_is_restored_to_the_original_value()
{
    var stream = new MemoryStream();
    var data = new byte[]{1,2,3,4,5,6,7,8,9,10};

    stream.GZip(data);

    var decompressed = new GZipStream(stream, CompressionMode.Decompress);

    var data2 = new byte[10];
    decompressed.Read(data2,0,10);

    Assert.That(data, Is.EqualTo(data2));
}

For more information see: http://msdn.microsoft.com/en-us/library/hh158301(v=vs.110).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文