473,327 Members | 2,016 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,327 software developers and data experts.

Need help with WebRequest and POST

I am trying to submit data to a webpage in the form of a post and my code is
below. It is a function that takes a url and the post content as strings
and then performs the post. But as soon as I add the post data to the
headers I get an exception that just says "headers". Can anyone please help
me here? Thanks....

public string Get(string u, string c)
{
WebRequest wr = WebRequest.Create(u);
wr.Headers.Add(c);
wr.Method="POST";
wr.ContentType = "text/xml";
wr.ContentLength = c.Length;
wr.Timeout = 5000;

StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream()) ;
string response = sr.ReadToEnd();
sr.Close();
return response;
}

Keith
Nov 15 '05 #1
2 3242
"Keith Selbee" wrote:
I am trying to submit data to a webpage in the form of a post and my
code is below. It is a function that takes a url and the post
content as strings and then performs the post. But as soon as I add
the post data to the headers I get an exception that just says
"headers". Can anyone please help me here? Thanks....

public string Get(string u, string c)
{
WebRequest wr = WebRequest.Create(u);
wr.Headers.Add(c);
wr.Method="POST";
wr.ContentType = "text/xml";
wr.ContentLength = c.Length;
wr.Timeout = 5000;

StreamReader sr = new
StreamReader(wr.GetResponse().GetResponseStream()) ; string response
= sr.ReadToEnd(); sr.Close();
return response;
}


You're posting an empty body and the wrong content length. I'm sure
that's not what you want to do ;-)

Assuming "c" is your POST data, it should be written to the request
stream, not put into the headers collection.

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 15 '05 #2
You don't add the post data to the headers. You add it to the body. I
attached sample code.
Hope this helps,

Randy
http://www.kbcafe.com

"Keith Selbee" <ks*****@nospam.neo.rr.com> wrote in message news:<eq**************@TK2MSFTNGP12.phx.gbl>...
I am trying to submit data to a webpage in the form of a post and my code is
below. It is a function that takes a url and the post content as strings
and then performs the post. But as soon as I add the post data to the
headers I get an exception that just says "headers". Can anyone please help
me here? Thanks....

/************************************************** ***
* writeToURL is a method that writes the contents of
* a specified URL to the web
************************************************** ***/
void writeToURL (WebRequest request, string data)
{

byte [] bytes = null;
// Get the data that is being posted (or sent) to the server
bytes = System.Text.Encoding.ASCII.GetBytes (data);
request.ContentLength = bytes.Length;
// 1. Get an output stream from the request object
Stream outputStream = request.GetRequestStream ();

// 2. Post the data out to the stream
outputStream.Write (bytes, 0, bytes.Length);

// 3. Close the output stream and send the data out to the web server
outputStream.Close ();
} // end writeToURL method

/************************************************** ***
* retrieveFromURL is a method that retrieves the contents of
* a specified URL in response to a request
************************************************** ***/
String retrieveFromURL (WebRequest request)
{
// 1. Get the Web Response Object from the request
WebResponse response = request.GetResponse();
// 2. Get the Stream Object from the response
Stream responseStream = response.GetResponseStream();

// 3. Create a stream reader and associate it with the stream object
StreamReader reader = new StreamReader (responseStream);

// 4. read the entire stream
return reader.ReadToEnd ();
}// end retrieveFromURL method

/************************************************** ***
* postToURL is a method that forces a POST
* of data to a specified URL
*
* A HTTP POST is a combination of a write to the Web Server
* and an immediate read from the Web server
************************************************** ***/
void postToURL (string url, string data)
{

// Create the Web Request Object
WebRequest request = WebRequest.Create(url);
// Specify that you want to POST data
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (data != null)
{
// write out the data to the web server
writeToURL (request, data);
}
else
{
request.ContentLength = 0;
}
// read the response from the Web Server
string htmlContent = retrieveFromURL (request);
} // end postToURL method
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul | last post by:
Hello, First I want to refer to the problem "WebRequest : execute a button" of a few days ago. The way I solved it, I loose my session, and as a consequence my session variables. I don't want...
1
by: Juan Dent | last post by:
Hi, The Request.SaveAs method stores in a file the complete HTTP request including the HttpVersion. However when I try to reproduce in memory the same thing, I cannot find the "HTTP/1.1"...
2
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html...
3
by: Stephane | last post by:
Hi, I'm trying to use PayPal and its Instant Payment Notification. In short, when a payment is made, PayPal send a post to my server and I post it back to PayPal. I'm using WebRequest to do...
0
by: MARTIN LANNY | last post by:
Hi everyone, To bring you in the picture: I have the function to grab the html content from the page. Function requires only URL and Time Delay variables. Url, which I am submitting to this...
0
by: Glenn Venzke | last post by:
I am trying to invoke a web service from an ASP.net page using the WebRequest object to an HTTP post. The web service is basically a print manager that prints sets of documents based an an invoice...
29
by: jens Jensen | last post by:
Hello, I got this "breath taking" task to write a an http server to which "xml data" will be posted to and will answer with xml data. The logic behind the xml processing is not a matter here. ...
1
by: Mr Flibble | last post by:
OK I logon to a web site and I manage to get an SMSESSION cookie that I then store in a variable called _session (a class scoping variable). I do this by calling a logon URL and setting a cookie...
5
by: jitsu | last post by:
Hi all, I have a problem. I need to connect to Web Server, send a xml string (query) to it and receive another xml string (result). I have the location of the Web Server like:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.