473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with HttpWebRequest

Hi,

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.Method="POST";
Step2Request.KeepAlive=true;
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.Referer=Global.LoginHostHTTP;
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.ContentType="application/x-www-form-urlencoded";
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;
Step2Request.ContentLength=Step2DataLength;

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


Thanks,

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

Expand|Select|Wrap|Line Numbers
  1. Stream REQstream = Step2Request.GetRequestStream();
  2.  
  3. //this line is what SENDS the request, yet you haven't put data on the stream
  4. HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
  5.  
  6. Stream RESstream = Step2Response.GetResponseStream();
  7. REQstream.Write(Step2UploadData,0,Step2DataLength) ;
  8. REQstream.Close();
  9.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. Stream REQstream = Step2Request.GetRequestStream();
  2. REQstream.Write(Step2UploadData,0,Step2DataLength) ;
  3. REQstream.Close();
  4.  
  5. HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
  6. Stream RESstream = Step2Response.GetResponseStream();
  7.  
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.

Thanks,

Nick
Nov 6 '07 #3
Plater
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

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

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
4
by: R Reyes | last post by:
I am trying to code a file uploader (for forum/email attachments) from the client computer to a remote web server via the PUT method (since POST is not allowed ). However, the upload works ONLY...
22
by: Kristof Thys | last post by:
Hello, I'm developing a C# - windows forms application. To get some information for my application, I'm connecting to an URL, wich gives me XML generated using php. With 90% of the users, this...
1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
5
by: japslam japslam via DotNetMonster.com | last post by:
Hi all, I have problem when I use HttpWebRequest and take long time to call to my service server. If at that time there are many request comes in semultaneous, I will get this exception ...
5
by: bg | last post by:
Hi all. I 'm trying to automate some of my workflow, by doing some programmatic "POST"s to an internal webserver via HttpWebRequest. The Code is your usual straight forward type of thing. ...
6
by: Georg | last post by:
Hello, I am trying to load a web page over a HTTP proxy with the POST method and I am using the following code: // open request (string url) HttpWebRequest httpWebRequest =...
0
by: davidpenty | last post by:
Hi there, I am having some problems with a multi-threaded asp.net seach page. My search page sends off four asynchronous http requests to four search engines then waits for the results to come...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.