Need help with WebRequest and POST

  • Thread starter Thread starter Keith Selbee
  • Start date Start date
K

Keith Selbee

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
 
Keith Selbee said:
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,
 
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 said:
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
 
Back
Top