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
3 1477
You appear to be doing things in the wrong order. -
Stream REQstream = Step2Request.GetRequestStream();
-
-
//this line is what SENDS the request, yet you haven't put data on the stream
-
HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
-
-
Stream RESstream = Step2Response.GetResponseStream();
-
REQstream.Write(Step2UploadData,0,Step2DataLength) ;
-
REQstream.Close();
-
Try this: -
Stream REQstream = Step2Request.GetRequestStream();
-
REQstream.Write(Step2UploadData,0,Step2DataLength) ;
-
REQstream.Close();
-
-
HttpWebResponse Step2Response = (HttpWebResponse)Step2Request.GetResponse();
-
Stream RESstream = Step2Response.GetResponseStream();
-
Also, you are aware that System.Text.Encoding.ASCII.GetBytes() can be used to convert a string to a byte[] ?
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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.
...
|
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 =...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
| |