469,943 Members | 2,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,943 developers. It's quick & easy.

Problem with HttpWebRequest


I was using the WebClient class (to do some screen scraping) and I was forced to change to the HttpWebRequest/Response because I needed control over the http headers. I am having two problems that have got me stumped. I am using Visual Studio 2003, Framework 1.1.

1) Even though I set HttpWebRequest.KeepAlive=true, I am not getting a connection header with keep-alive as the value. According to everything I have read this should work.

2) My Request stream is not being sent to the destination server.

Any help would be appreciated, here is the code:

HttpWebRequest Step2Request = (HttpWebRequest)WebRequest.Create(Global.LoginHost HTTP);
Step2Request.Accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
Step2Request.UserAgent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)";
Step2Request.Headers.Add("UA-CPU: x86");
Step2Request.Headers.Add("Accept-Encoding: gzip, deflate");
Step2Request.Headers.Add("Accept-Language: en-us");
Step2Request.Headers.Add("Cache-Control: no-cache");
byte [] Step2UploadData=CommonMethodsClass.convertstringto byte(querystring);
int Step2DataLength=Step2UploadData.Length;

Stream REQstream = Step2Request.GetRequestStream();
HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
Stream RESstream = Step2Response.GetResponseStream();
REQstream.Write(Step2UploadData,0,Step2DataLength) ;


Nov 4 '07 #1
3 1299
7,872 Expert 4TB
You appear to be doing things in the wrong order.

Expand|Select|Wrap|Line Numbers
  1. Stream REQstream = Step2Request.GetRequestStream();
  3. //this line is what SENDS the request, yet you haven't put data on the stream
  4. HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
  6. Stream RESstream = Step2Response.GetResponseStream();
  7. REQstream.Write(Step2UploadData,0,Step2DataLength) ;
  8. REQstream.Close();
Try this:
Expand|Select|Wrap|Line Numbers
  1. Stream REQstream = Step2Request.GetRequestStream();
  2. REQstream.Write(Step2UploadData,0,Step2DataLength) ;
  3. REQstream.Close();
  5. HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
  6. Stream RESstream = Step2Response.GetResponseStream();
Also, you are aware that System.Text.Encoding.ASCII.GetBytes() can be used to convert a string to a byte[] ?
Nov 5 '07 #2
Thanks for the response. Yes, I realized right after I posted this that I had goofed with the order. Sorry about that. I had been converting the data to a byte array but it seemed to work without that extra line of code, so let me know if it is more efficient with or without and I'll include it. I was doing it that way with the WebClient.Uploaddata method. My real problem is the "Connection: Keep-Alive" header. I don't understand why setting it to true doesn't cause the connection header to appear. I wish I knew how to get the connection header to appear using ProtocolVersion 1.1.


Nov 6 '07 #3
7,872 Expert 4TB
I thought this:
CommonMethodsClass.convertstringtobyte(querystring );
Was a custom class built class that all it did was convert a string to a byte[].
I was suggesting you could just use a built in method.

The Connection header, I believe, is only valid for http/1.1 I'm also unsure how well it works with the HttpWebRequest object.
Nov 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by TJO | last post: by
1 post views Thread by Satinderpal Singh | last post: by
4 posts views Thread by R Reyes | last post: by
1 post views Thread by Satinderpal Singh | last post: by
5 posts views Thread by japslam japslam via DotNetMonster.com | last post: by
5 posts views Thread by bg | last post: by
6 posts views Thread by Georg | last post: by
reply views Thread by davidpenty | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.