Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hit external url from code-behind

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?

like image 361
Steven Avatar asked Apr 12 '26 09:04

Steven


2 Answers

    string line;
    HttpWebRequest request = WebRequest.Create("http://www.yahoo.com") as HttpWebRequest;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    StreamReader streamr = new StreamReader(response.GetResponseStream());
    line = streamr.ReadToEnd();

can u get to ma post i have written all over der

like image 80
Kiran Solkar Avatar answered Apr 14 '26 22:04

Kiran Solkar


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();
like image 28
Aaron Avatar answered Apr 14 '26 23:04

Aaron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!