1
votes

I am trying to login programetically using the following code, Untill two days before it worked fine, but now suddenly i am getting a WebException "The remote server returned an error: (417) Expectation Failed - " How to resolve this problem.

I also tried the solution posted in Why do I receive this error: The remote server returned an error: (417) Expectation Failed

Can any one tell me how to resolve this.

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

Statement causing Exception

HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
1
If I inspect the GET with fiddler I see at least two cookies getting set. I assume they are relevant. Use a CookieContainer to obtain and hold cookies for subsequent requests.rene
code looks fine...there might be some server issue. and exception is also occuring when server call is initiated.CodeSpread
@rene I also tried CookieContainer it didn't worked, But the same code is worked two days before. And Here i don't want any subsequent requests? My requirement is only to login.user1870466
@user1870466 What is ContentType ?L.B
@L.B ContentType = "application/x-www-form-urlencoded";user1870466

1 Answers

1
votes

Thanks for all who spend their time to answer my question.

I tried the solution posted in http-post-returns-the-error-417-expectation-failed-c It worked for me.

I just need to add following code

System.Net.ServicePointManager.Expect100Continue = false;