Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending POST data with C#

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 :)

like image 911
Wen Avatar asked Nov 29 '22 17:11

Wen


2 Answers

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

like image 89
Geek Avatar answered Dec 06 '22 07:12

Geek


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());
like image 24
Adam Robinson Avatar answered Dec 06 '22 08:12

Adam Robinson