C# - Webclient Post Data

孤者浪人 提交于 2019-12-11 13:01:52

问题


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

I use it like:

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??


回答1:


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


来源:https://stackoverflow.com/questions/13933987/c-sharp-webclient-post-data

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