473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with HttpWebRequest

77 New Member
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 1482
Plater
7,872 Recognized Expert Expert
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
stoogots2
77 New Member
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 Recognized Expert Expert
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.