2015-06-27 93 views
0

我想登錄到網站:https://secure.tibia.com/account/?subtopic=accountmanagement在C#WinForms中使用HTTPWebRequest。我目前不知道它是否登錄。我希望它能檢查我是否登錄過。HTTP請求登錄到某個網站,並檢查它是否有效

當您在網站上輸入無效的用戶名/密碼時,它會顯示一條錯誤消息。是否有某種方式來檢查是否出現錯誤消息,然後讓消息框顯示「錯誤的登錄信息!」 ?

可以檢查的字符串是The following error has occurred。如果在嘗試登錄後出現在網站上,我們知道它失敗了。然後它應該創建一個消息框,說「登錄錯誤」或什麼的。

這是我目前使用的代碼。我的用戶名和密碼存儲在「帳戶」和「密碼」變量中。請注意,我仍然不知道這是否是登錄的正確方式。用戶名框稱爲「登錄名」,密碼爲「loginpassword」。但我不知道是否我應該像鍵入它作爲代碼:

private void OnPostInfoClick(object sender, System.EventArgs e) 
    { 
     string strAcc = account; 
     string strPass = password; 

     ASCIIEncoding encoding = new ASCIIEncoding(); 
     string postData = "loginname=" + strAcc; 
     postData += ("&loginpassword=" + strPass); 
     byte[] data = encoding.GetBytes(postData); 

     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://secure.tibia.com/account/?subtopic=accountmanagement"); 

     myRequest.Method = "POST"; 

     myRequest.ContentType = "application/x-www-form-urlencoded"; 
     myRequest.ContentLength = data.Length; 
     Stream newStream = myRequest.GetRequestStream(); 
     // Send the data. 
     newStream.Write(data, 0, data.Length); 
     newStream.Close(); 
    } 

回答

1

HttpWebResponse響應=(HttpWebResponse)myRequest.GetResponse(); If(response.StatusCode.ToString()==「401」)