This is the method that I'm trying to use to send POST data to a URL and bring back its response:
public string sendPOST(string URL, string postData)
{
byte[] byteArray;
Stream webpageStream;
StreamReader webpageReader;
String webpageContent;
byteArray = Encoding.UTF8.GetBytes(postData);
_webRequest = WebRequest.Create(URL);
_webRequest.Method = "POST";
_webRequest.ContentType = "application/x-www-form-urlencoded";
_webRequest.ContentLength = byteArray.Length;
webpageStream = _webRequest.GetResponse().GetResponseStream();
webpageStream.Write(byteArray, 0, byteArray.Length);
webpageStream.Close();
webpageReader = new StreamReader(webpageStream);
webpageContent = webpageReader.ReadToEnd();
return webpageContent;
}
I got a lot of this code from the MSDN web page so i know I'm roughly on the right track... but when I call the method using:
string test = webHelper.sendPOST("http://google.com", "var=1");
MessageBox.Show(test);
The application just locks up. I have debugged the method and as far as I can see the code runs fine up till this line:
webpageStream = _webRequest.GetResponse().GetResponseStream();
I have tried wrapping it up in a try block but no exeptions are thrown at all.
Does anyone have enough experience with web requests to help me out?
Thanks a lot :)