2011-11-26 52 views
-5

我已經嘗試了很多方式以編程方式登錄到https網站,但我遇到了問題。每當我收到錯誤消息,說明我的登錄名和密碼不正確。我相信他們是正確的,因爲我可以使用相同的憑據通過瀏覽器登錄到網站。HttpWebRequest,C#和Https

失敗代碼

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1"); 
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0"; 
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8"; 
request.UnsafeAuthenticatedConnectionSharing = true; 
request.Method = "POST"; 
request.KeepAlive = true; 
request.ContentType = "application/x-www-form-urlencoded"; 
request.AllowAutoRedirect = true; 
request.CookieContainer = container; 

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

//String tmp; 
foreach(Cookie cookie1 in response.Cookies) 
{ 
    container.Add(cookie1); 
} 

Stream stream = response.GetResponseStream(); 

string html = new StreamReader(stream).ReadToEnd(); 
Console.WriteLine("" + html); 
+0

你的代碼在哪裏設置憑據?所有註釋代碼的目的是什麼?這與問題有關嗎?如果沒有,請刪除它。 –

+0

顯然你不是在做BASIC認證,所以我不能真正告訴你需要設置哪些字段,因爲我不知道這個網站是什麼。同時請格式化並清理此代碼,這在目前的形式中非常混亂。 –

回答

0

您正在嘗試發佈的東西(我不明白,是什麼,從你的代碼),但沒有憑據。我想你的網頁會顯示你輸入用戶名(電子郵件地址?)和密碼的網頁表單。然後瀏覽器發佈此表格。因此,您需要複製瀏覽器行爲 - 對錶單內容進行編碼並將其發送到您的發佈請求中。使用一些常用瀏覽器的網站管理員開發工具來查看客戶端瀏覽器發送給服務器的具體內容以及它如何編碼表單數據。接下來,很可能您的請求需要特殊的Cookie,您可以通過訪問其他頁面(例如登錄頁面)來收集這些Cookie。發送預設cookie(如您在評論代碼中執行的操作)不適用於大多數網站。

換言之,適當的機制是:

  1. GET登錄網頁
  2. 收集餅乾
  3. POST形式數據,並在請求通過收集區。
  4. 收集其他可能在登錄後發送的cookie。
+0

我正在嘗試這種方式,但這不是工作。第一個解決方案正在工作(以字節數組形式發送登錄數據)。謝謝。 – jars

+0

@jars,我認爲沒有調試你的代碼並檢查你的特定網站,沒有人能夠幫助你。所以你在這裏運氣不好。 –

1

該網站使用HTTP POST進行登錄,並且不會在URL中發送用戶名和密碼。

正確的登錄URL是https://www.majesticseo.com/account/login

您需要創建數據後,將其轉換爲字節數組的字符串,設置內容的長度,然後做你的要求。發送內容長度非常重要。沒有它,該帖子將無法使用。

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.majesticseo.com/account/login?EmailAddress=myemail&Password=mypass&RememberMe=1"); 

     request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0"; 
     request.Referer = "https://www.majesticseo.com/account/login"; 
     request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8"; 
     request.UnsafeAuthenticatedConnectionSharing = true; 
     request.Method = "POST"; 
     request.KeepAlive = true; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.AllowAutoRedirect = true; 

     // the post string for login form 
     string postData = "redirect=&EmailAddress=EMAIL&Password=PASS"; 
     byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(postData); 

     request.ContentLength = postBytes.Length; 

     System.IO.Stream str = request.GetRequestStream(); 

     str.Write(postBytes, 0, postBytes.Length); 

     str.Close(); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     System.IO.Stream stream = response.GetResponseStream(); 


     string html = new System.IO.StreamReader(stream).ReadToEnd(); 

     Console.WriteLine("" + html);