Monday, March 26, 2012

Trouble Performing An HTTP POST

Hi there. I'm having trouble with an HTTP Post in my code behind. Can
anyone help?
Here's my code:
string data = "&fields_fname = " + txtFirstName.Text + "&fields_lname="
+ txtLastName.Text;
string url = "http://localhost/app/default.asp";
WebRequest objWebRequest = WebRequest.Create(url);
objWebRequest.Method = "POST";
objWebRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes (data);
objWebRequest.ContentLength = bytes.Length;
Stream outputStream = objWebRequest.GetRequestStream();
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Close ();Whats the trouble your having with it?
The first thing I notice is that you start your data with a &, you dont
need one in front.
Other than that, whats the problem?
Sean
rh120...@.gmail.com wrote:
> Hi there. I'm having trouble with an HTTP Post in my code behind. Can
> anyone help?
> Here's my code:
>
> string data = "&fields_fname = " + txtFirstName.Text + "&fields_lname="
> + txtLastName.Text;
> string url = "http://localhost/app/default.asp";
> WebRequest objWebRequest = WebRequest.Create(url);
> objWebRequest.Method = "POST";
> objWebRequest.ContentType = "application/x-www-form-urlencoded";
> byte[] bytes = System.Text.Encoding.ASCII.GetBytes (data);
> objWebRequest.ContentLength = bytes.Length;
> Stream outputStream = objWebRequest.GetRequestStream();
> outputStream.Write (bytes, 0, bytes.Length);
> outputStream.Close ();

0 comments:

Post a Comment