views:

56

answers:

4

I have a form on my site. The user enters their e-mail and selects a location from a dropdown. I then need to post that data to an external site by hitting a url with the user's location and e-mail in the query string.

I'm doing this like so:

string url = "http://www.site.com/page.aspx?location=" + location.Text + "&email=" + email.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

My client says that I am not hitting their server, but when going through the debugger, I'm getting a response from their server. I also tried tracking what was happening by using Firebug, and I noticed that there was no POST made to that external site.

What am I doing wrong here?

+1  A: 

How is it triggered at the client side?

Humberto
This is not an answer, so you should have posted it as a comment to the question.
Oded
I'm not able to post it as a comment, sorry.
Humberto
+1 to help give you commenting privileges.
Aaron
Great Aaron, I'm very thankful.
Humberto
+2  A: 

Make sure you're doing a POST and not a GET method. This is some similar code that I've used in the past.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);        
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;
                request.Method = "POST";
                request.Timeout = 30000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Aaron
This worked great, thank you.
Steven
+1  A: 

Check what Method you are using for the WebRequest. I am assuming it defaults to a GET, not a POST.

You can easily set it yourself:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Oded
A: 

From the code I see here, I would have to say your client is wrong, you are hitting their server (assuming site.com is their server and you have a working internet connection :). However, if you need to do a POST instead of a GET to send the data, perhaps something like the following:

string url = "http://www.site.com/page.aspx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

// set request properties
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// set post values
string postValues = "location=" + location.Text + "&email=" + email.Text;
request.ContentLength = postValues.Length;

// write post values
StreamWriter streamWriter = new StreamWriter (request.GetRequestStream(), System.Text.Encoding.ASCII);
streamWriter.Write(postValues);
streamWriter.Close();

// process response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
string responseData = streamReader.ReadToEnd();
streamReader.Close();

// do any processing needed on responseData...
Nate Pinchot