如何压缩 HttpWebRequest POST
我正在尝试将数据发布到接受压缩数据的服务器。下面的代码工作得很好,但它是未压缩的。我以前没有使用过压缩或 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要回答您提出的问题,要发布压缩数据,您所需要做的就是用 gzip 流包装请求流,
这与请求 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
This is completely different than requesting a gzip response, which is a much more common thing to do.
我还使用类似于 tnyfst 的代码收到了“在写入所有字节之前无法关闭流”错误。问题是我有:
binData 是压缩前的原始数据。显然,压缩内容的长度会有所不同,因此我删除了这一行并最终得到以下代码:
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:
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:
在
Page_Load
事件中:对于发出压缩请求:
HttpWebRequest 和GZip Http 响应 作者:Rick Strahl
In
Page_Load
event:And for making compressed requests:
HttpWebRequest and GZip Http Responses by Rick Strahl
尝试一下这个扩展方法。
该流将保持打开状态(请参阅 GZipStream 构造函数)。
压缩完成后,流位置设置为 0。
您可以使用以下测试:
有关详细信息,请参阅: 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.
You can use the following test:
For more information see: http://msdn.microsoft.com/en-us/library/hh158301(v=vs.110).aspx