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 :)
I realise this is an old question but thought I would provide an answer with correct usage of using statements so the closing of connections is done without the need of calling 'Close'. Also the webrequest can be declared within the scope of the SendPost method.
public string SendPost(string url, string postData)
{
string webpageContent = string.Empty;
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
using (Stream webpageStream = webRequest.GetRequestStream())
{
webpageStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
webpageContent = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
//throw or return an appropriate response/exception
}
return webpageContent;
}
This is also following this answer https://stackoverflow.com/a/336420/453798
Hope this helps
You have a logic error in your code:
webpageStream.Close();
webpageReader = new StreamReader(webpageStream);
You're closing the stream, then trying to read from it. Once a stream is closed, it's effectively dead.
The more fundamental problem is that you're trying to write your request to the response, which is not only nonsensical, but also impossible! What you want to do is write to the request stream, then get the response like this:
webpageStream = _webRequest.GetRequestStream();
webpageStream.Write(byteArray, 0, byteArray.Length);
webpageStream.Close();
webpageReader = new StreamReader(_webRequest.GetResponse().GetResponseStream());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With