HttpWebRequest GET with no Gzip?

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();
 
J

Joerg Jooss

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top