2013-10-14 209 views
0

我想通過POST使用httpwebrequest登錄網站(例如:https://www.facebook.com/login.php?login_attempt=1)。但登錄後不會返回結果。我無法使用httpwebrequest登錄網站

我不知道我的代碼有什麼問題,你能幫我嗎?非常感謝!

這是我的代碼:

 private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     System.Uri myUri = new System.Uri("https://www.facebook.com/login.php?login_attempt=1"); 
     HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri); 
     myRequest.Method = "POST"; 
     myRequest.ContentType = "application/x-www-form-urlencoded"; 
     myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest); 

    } 

    void GetRequestStreamCallback(IAsyncResult callbackResult) 
    { 
      HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState; 
      // End the stream request operation 
      Stream postStream = myRequest.EndGetRequestStream(callbackResult); 
      StringBuilder postData = new StringBuilder(); 
      postData.Append("email=myEmail"); 
      postData.Append("&password=myPassword"); 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString()); 
      postStream.Write(byteArray, 0, postData.Length); 
      postStream.Close(); 
      myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest); 
    } 
    void GetResponsetStreamCallback(IAsyncResult callbackResult) 
    { 
      HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState; 
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult); 
      using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream())) 
      { 
       string result = httpWebStreamReader.ReadToEnd(); 
       //For debug: show results 
       Deployment.Current.Dispatcher.BeginInvoke(() => 
       { 
        // something do 
       }); 
      } 

    } 
+0

那麼它返回什麼?在哪個方法?所有三種方法都打了嗎?你的調試器是說嗎? –

+0

這是返回登錄頁面(所以不能登錄)。我找不到錯誤,基本上是從http://transoceanic.blogspot.com/2011/09/wp7-sending-post-data-and-receive複製的示例。 html –

回答

0

我剛剛快看:https://www.facebook.com/login.php的輸入字段被命名爲‘電子郵件’和‘通’(而不是‘密碼’)。 未經測試:更改

postData.Append("&password=myPassword"); 

postData.Append("&pass=myPassword"); 

即使這個工程,我懷疑你會得到更進一步,不管你打算這樣做。我建議使用其中一個Facebook SDK。

+0

這是我的項目的一個例子,我使用localhost作爲我的應用程序:http://localhost/moodle/login/index.php輸入字段是名稱「用戶名」和「密碼」,但它仍然沒有運行。我正在嘗試更多的addrees,但不是成功的。請幫幫我! –

+0

您的代碼在第一次發佈請求Facebook。如果它是在您的機器上請求服務,那麼您應該調試此服務以瞭解登錄失敗的原因。 –