生成HTTP POST请求时出错?

时间:2012-12-02 15:02:20

标签: c# asp.net httpwebrequest

我正在尝试使用以下代码以编程方式登录,直到它工作正常两天,但现在我突然得到一个WebException “远程服务器返回错误:(417)期望失败 - ”如何解决这个问题。

我也尝试了在Why do I receive this error: The remote server returned an error: (417) Expectation Failed

中发布的解决方案

任何人都可以告诉我如何解决这个问题。

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

1 个答案:

答案 0 :(得分:1)

感谢所有花时间回答我问题的人。

我尝试了http-post-returns-the-error-417-expectation-failed-c中发布的解决方案 它对我有用。

我只需要添加以下代码

System.Net.ServicePointManager.Expect100Continue = false;