使用C#发送POST数据 [英] Sending POST data with C#

查看:2084
本文介绍了使用C#发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试用于将POST数据发送到URL并返回其响应的方法:

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;
}

我从MSDN网页上获得了很多这样的代码,所以我知道我大致上是正确的……但是当我使用以下方法调用该方法时:

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

我尝试将其包装在try块中,但是没有抛出任何异常.

I have tried wrapping it up in a try block but no exeptions are thrown at all.

有人对Web请求有足够的经验来帮助我吗?

Does anyone have enough experience with web requests to help me out?

非常感谢:)

推荐答案

我意识到这是一个老问题,但我想我会提供正确使用using语句的答案,因此无需调用'关闭'.同样,可以在SendPost方法的范围内声明webrequest.

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;
}

这也遵循此答案 https://stackoverflow.com/a/336420/453798

希望这会有所帮助

这篇关于使用C#发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆