我想將數據發佈到接受壓縮數據的服務器。下面的代碼工作得很好,但它是未壓縮的。我沒有使用壓縮或Gzip beofre,所以任何幫助appriciated。如何壓縮HttpWebRequest POST
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整個字節數組嗎?我是否需要添加其他標題或刪除已存在的標題?
謝謝!
斯科特
鏈接的文章解釋瞭如何解壓縮在一個響應返回的數據,而不是如何構建請求使用壓縮數據。 – Scott 2010-11-11 19:47:30
@Scott更新。 – 2010-11-12 06:28:12