Sending POST data with C#

亡梦爱人 提交于 2019-11-28 01:33:17

问题


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


回答1:


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




回答2:


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());


来源:https://stackoverflow.com/questions/4718420/sending-post-data-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!