I am sending and receiving web requests with HttpWebRequest.
After a response it is usually gzip’d content encoding.
Some computers will receive deflate encoding.
Some other computers will receive identity encoding.
I have it set up to read gzip and deflate encoding but am unsure how to read identity encoding.
string ReturnString = "";
HttpWebRequest HttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebRequest.ProtocolVersion = Version.Parse("1.1");
WebHeaderCollection WebHeaderCollection = HttpWebRequest.Headers;
HttpWebRequest.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
WebHeaderCollection.Add("Accept-Language: en-us");
WebHeaderCollection.Add("Accept-Encoding: gzip, deflate");
HttpWebRequest.KeepAlive = true;
HttpWebResponse HttpWebResponse = (HttpWebResponse)HttpWebRequest.GetResponse();
using (var mem = HttpWebResponse.GetResponseStream())
{
if (HttpWebResponse.ContentEncoding.ToLower().Contains("gzip"))
{
using (var gzip = new GZipStream(mem, CompressionMode.Decompress))
{
using (var reader = new StreamReader(gzip))
{
ReturnString = reader.ReadToEnd();
}
}
}
else if (HttpWebResponse.ContentEncoding.ToLower().Contains("deflate"))
{
using (var gzip = new DeflateStream(mem, CompressionMode.Decompress))
{
using (var reader = new StreamReader(gzip))
{
ReturnString = reader.ReadToEnd();
}
}
}
}
WebHeaderCollection ResponseHeaders = HttpWebResponse.Headers;
HttpWebResponse.Close();
Edit:
Another PC received “Transfer-Encoding: chunked” and no Content Encoding, which if I read it right it should replace Content Encoding. http://en.wikipedia.org/wiki/Chunked_transfer_encoding.
Is there no way to catch any type of encoding?
RFC:
As for your edit: you should not have to care about the response being chunked: using
string resp = new StreamReader(response.GetResponseStream()).ReadToEnd()should give you all the response data.