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******* *******@TK2MSFT NGP12.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.Enc oding.ASCII.Get Bytes (data);
request.Content Length = bytes.Length;
// 1. Get an output stream from the request object
Stream outputStream = request.GetRequ estStream ();
// 2. Post the data out to the stream
outputStream.Wr ite (bytes, 0, bytes.Length);
// 3. Close the output stream and send the data out to the web server
outputStream.Cl ose ();
} // 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.GetResp onse();
// 2. Get the Stream Object from the response
Stream responseStream = response.GetRes ponseStream();
// 3. Create a stream reader and associate it with the stream object
StreamReader reader = new StreamReader (responseStream );
// 4. read the entire stream
return reader.ReadToEn d ();
}// 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.Crea te(url);
// Specify that you want to POST data
request.Method = "POST";
request.Content Type = "applicatio n/x-www-form-urlencoded";
if (data != null)
{
// write out the data to the web server
writeToURL (request, data);
}
else
{
request.Content Length = 0;
}
// read the response from the Web Server
string htmlContent = retrieveFromURL (request);
} // end postToURL method