2012-12-02 58 views
1

我想使用下面的代碼進行programetically登錄,直到它工作正常前兩天,但現在突然我得到一個WebException 「遠程服務器返回錯誤:(417)期望失敗 - 」如何解決此問題問題。生成HTTP POST請求時出錯?

我也試過張貼在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

。我認爲他們是相關的。使用CookieContainer獲取並保存隨後請求的Cookie。 – rene

+0

代碼看起來很好......可能會有一些服務器問題。並且在啓動服務器調用時也會發生異常。 – CodeSpread

+0

@rene我也試過CookieContainer它沒有工作,但是相同的代碼在兩天前工作。在這裏,我不想要任何後續請求?我的要求只是登錄。 – user1870466

回答