HttpWebRequest GET with no Gzip?

  • Thread starter Thread starter poi
  • Start date Start date
P

poi

I want to do an HTTP "GET" against a remote web server, and make sure
that I get data back with gzip so the transfer is as fast as possible.
It seems like gzip encoding only works with a "POST", and I can't
"POST".

This throws an exception
"An unhandled exception of type 'System.Net.ProtocolViolationException'
occurred in system.dll
Additional information: Content-Length cannot be set for a non-write
operation."



wR = (HttpWebRequest)WebRequest.Create( myUri );
wR.Timeout = 3000;
wR.UserAgent = "SiteSee 1.0";
wR.Method = "GET";
wR.ContentType = "application/x-www-form-urlencoded";
wR.AllowWriteStreamBuffering = false;
wR.KeepAlive = true;
wR.ProtocolVersion = HttpVersion.Version11;
wR.SendChunked = true;
wR.TransferEncoding = "gzip";
wR.ContentLength = 200000;
HttpWebResponse webResponse = (HttpWebResponse)wR.GetResponse();
System.Text.Encoding defaultEncoding =
System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(
webResponse.GetResponseStream() , defaultEncoding );
string siteResponse = responseStream.ReadToEnd();
webResponse.Close();
responseStream.Close();
 
poi said:
I want to do an HTTP "GET" against a remote web server, and make sure
that I get data back with gzip so the transfer is as fast as
possible. It seems like gzip encoding only works with a "POST", and
I can't "POST".

This throws an exception
"An unhandled exception of type
'System.Net.ProtocolViolationException' occurred in system.dll
Additional information: Content-Length cannot be set for a non-write
operation."

As usual, the system is right ;-)
wR = (HttpWebRequest)WebRequest.Create( myUri );
wR.Timeout = 3000;
wR.UserAgent = "SiteSee 1.0";
wR.Method = "GET";
wR.ContentType = "application/x-www-form-urlencoded";

That is not a harmful error, but since there's no body to sent with GET,
there can be no Content-Type.
wR.AllowWriteStreamBuffering = false;
wR.KeepAlive = true;
wR.ProtocolVersion = HttpVersion.Version11;
wR.SendChunked = true;
wR.TransferEncoding = "gzip";

That's wrong. What you're looking for is Content-Encoding, not Transfer-
Encoding. The only allowed value for this header in HTTP 1.1 is
"chunked".
wR.ContentLength = 200000;

That doesn't make sense -- Transfer-Encoding: chunked and Content-Length
are mutually exlusive options.
HttpWebResponse webResponse = (HttpWebResponse)wR.GetResponse();
System.Text.Encoding defaultEncoding =
System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(
webResponse.GetResponseStream() , defaultEncoding );
string siteResponse = responseStream.ReadToEnd();
webResponse.Close();
responseStream.Close();

What you're looking for is

wR.Headers.Add("Accept-Encoding: gzip");

If the server supports this, it will apply compression to the HTTP
response's body. But you still need to supply code to decompress the
body, as this is not handled automatically by the framework.

Cheers,
 
Back
Top