C# - Webclient发布数据

时间:2012-12-18 13:23:37

标签: c# post webclient

我正在尝试复制网站帖子,如下所示: https://www.usbank.com/cgi_w/cfm/personal/products_and_services/reoPropertiesReq.cfm

我只想发布任何州,以查看结果,因为它们最终会在电子邮件中显示。 这是我的post方法,它适用于其他网站上的登录等,所以我知道它有效

public HtmlDocument POST(string url, string postData)
    {//string myParameters = "param1=value1&param2=value2&param3=value3";

        HtmlDocument hdoc = new HtmlDocument();
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        using (wc)
        {
            hdoc.LoadHtml(wc.UploadString(url, postData));
        }

        return hdoc;
    }

我用它像:

HtmlDocument mainDoc = POST("https://www.usbank.com/cgi_w/cfm/personal/products_and_services/reoPropertiesReq.cfm",
            "selState=4&StateNM=Arizona");

但这似乎不正确。任何人都可以分析这个网站,并确定它是我的代码还是缺少数据?

1 个答案:

答案 0 :(得分:1)

我前段时间为我写过这个函数,希望这有帮助

    private void POST(string url, string data)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
        req.Method = "POST";
        req.Headers.Add(HttpRequestHeader.AcceptLanguage, "de-DE,de;q=0.8,en-US;q=0.7,en;q=0.3");

        req.Timeout = req.ReadWriteTimeout = 15000;

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] dataBytes = encoding.GetBytes(data);
        req.ContentLength = dataBytes.Length;
        Stream stream = req.GetRequestStream();
        stream.Write(dataBytes, 0, dataBytes.Length);
        stream.Close();

        req.GetResponse();
    }