问题
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¶m2=value2¶m3=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