By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

HttpWebRequest GET with no Gzip?

P: n/a
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();

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
poi wrote:

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,

--
Joerg Jooss
jo*********@gmx.net
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.