C#-Webclient发布数据 [英] C# - Webclient Post Data

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

问题描述

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

Im trying to replicate a site post as here: https://www.usbank.com/cgi_w/cfm/personal/products_and_services/reoPropertiesReq.cfm

我只想发布任何状态以查看结果,因为结果将最终以电子邮件形式发送. 这是我的发布方法,可以在其他网站上登录等,所以我知道它可以使用

I just want to post any state to see the results as they will end up in an email. This is my post method and it works for login etc on other sites so I know it works

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

但这似乎不正确.谁能分析这个网站并确定它是我的代码还是丢失的数据?

But this seems incorrect. Can anyone analyse this site and identify if its my code or missing data??

推荐答案

我已经为我编写了此函数,希望对您有所帮助

I've written this function for me some time ago, hope this helps

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

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

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