C#WebRequest GET / POST

时间:2014-10-10 21:18:47

标签: c# webrequest

所以我认为现在是时候学习C#了,对我们来说很容易,我对此非常陌生。

我试图创建一个非常简单的应用程序(我使用的是Windows Forms Application)。 我的目标是:

  1. 使用" GET"方法,获取网页
  2. 阅读文本字段(每次用户访问页面时此值都会更改
  3. 使用" POST"方法,相应地发送一些值
  4. 到目前为止,这是我的代码:

      private void button2_Click(object sender, EventArgs e)
    {
        string URI = "http://localhost/post.php";
        string myParameters = "field=value1&field2=value2";
    
        using (WebClient wc = new WebClient())
        {
            string getpage = wc.DownloadString("http://localhost/post.php");
            MessageBox.Show(getpage);
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HtmlResult = wc.UploadString(URI, myParameters);
            MessageBox.Show(HtmlResult);
        }
    }
    

    到目前为止一直很好,它正在工作,但它并不完全是我想要在这里实现的。 我能够使用POST方法,但如何在发送数据之前使用GET? 我想根据GET结果发送数据。

    如果我应该更好地描述我想要做的事情,请告诉我。

    感谢。

    修改
    这是我的PHP代码:

    <?php
    
        $a = session_id();
    
        if(empty($a))
            session_start();
    
            echo "Session: ".session_id()."<br/>\n";
    

    现在,回到我的C#代码,我在两条消息中获得了不同的会话ID

3 个答案:

答案 0 :(得分:13)

使用GET读取数据

  

请参阅此答案:Easiest way to read from a URL into a string in .NET

using(WebClient client = new WebClient()) {
    string s = client.DownloadString(url);
}

会话

默认情况下,WebClient不使用任何会话。因此,每个调用都会像处理您创建的新会话一样处理。要做到这一点,你需要这样的东西:

  

请参考以下答案:

     
      
  1. Using CookieContainer with WebClient class

  2.   
  3. Reading Response From URL using HTTP WEB REQUEST

  4.   

示例代码

public class CookieAwareWebClient : WebClient
{
    private readonly CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = m_container;
        }
        return request;
    }
}

// ...

private void button2_Click(object sender, EventArgs e)
{
    string URI = "http://localhost/post.php";
    string myParameters = "field=value1&field2=value2";

    using (WebClient wc = new CookieAwareWebClient())
    {
        string getpage = wc.DownloadString("http://localhost/post.php");
        MessageBox.Show(getpage);
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        string HtmlResult = wc.UploadString(URI, myParameters);
        MessageBox.Show(HtmlResult);
    }
}

答案 1 :(得分:3)

GET方法实际上只是通过地址行传递的参数,只需将您的请求发送到string.Format("{0}?{1}", URI, myParameters)(或URI + "?" + myParameters),然后只需阅读回复。

答案 2 :(得分:0)

我很久以前就知道了,但是:

       byte[] readedData = null;

        await Task.Run(async() =>
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadProgressChanged += (obj, args) => progessChangedAction?.Invoke(args.ProgressPercentage);

                readedData = await client.DownloadDataTaskAsync(requestUri).ConfigureAwait(false);
            }
        });

        return readedData;

}

相关问题