我正在尝试使用以下代码以编程方式登录,直到两天前它工作正常,但现在突然我收到一个WebException"远程服务器返回错误:(417)期望失败 - "如何解决此问题。
我还尝试了为什么会收到此错误中发布的解决方案:远程服务器返回错误:(417) 预期失败
任何人都可以告诉我如何解决这个问题。
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://sms.ultoo.com/login.php");
String Postdata = "LoginMobile=9999999999&LoginPassword=aaa";
webRequest.Timeout = 10 * 60 * 1000;
webRequest.Method = "POST";
webRequest.ContentType = ContentType;
webRequest.ContentLength = Postdata.Length;
// POST the data
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(Postdata);
}
// This actually does the request and gets the response back
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
string ResponseData = string.Empty;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// dumps the HTML from the response into a string variable
ResponseData = responseReader.ReadToEnd();
}
导致异常的语句
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
感谢所有花时间回答我问题的人。
我尝试了 http-post-returns-the-error-417-expectation-failed-c 中发布的解决方案它对我有用。
我只需要添加以下代码
System.Net.ServicePointManager.Expect100Continue = false;