2010-07-29 85 views

回答

4

像這樣的東西應該可以工作......你需要做一個http post請求併發送你的用戶名和密碼。要保持「登錄狀態」,您需要一個cookie容器,然後您可以將其重新用於所有其他請求。

編輯:已更新的測試代碼。在進行POST登錄之前,需要使用初始的GET請求來登錄/登錄會話cookie。我還添加了一些HTTP標頭,使其與瀏覽器的「正常」請求相同。

void Main() 
{ 
    //We need a container to store the cookies in. 
    CookieContainer cookies = new CookieContainer(); 

    //Request login page to get a session cookie 
    GETHtml("http://www.purevolume.com/login", cookies); 

    //Now we can do login 
    Login("some-user-name", "some-password", cookies); 
} 

public bool Login(string Username, string Password, CookieContainer cookies) 
{ 
    string poststring = string.Format("username={0}&password={1}&user_login_button.x=63&user_login_button.y=13&user_login_button=login", 
           Username, Password); 
    byte[] postdata = Encoding.UTF8.GetBytes(poststring); 

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.purevolume.com/login"); 
    webRequest.CookieContainer = cookies; 
    webRequest.Method = "POST"; 
    webRequest.Referer = "http://www.purevolume.com/login"; 
    webRequest.Headers.Add("origin", "http://www.purevolume.com"); 
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;"; 
    webRequest.ContentType = "application/x-www-form-urlencoded"; 
    webRequest.ContentLength = postdata.Length; 
    using (Stream writer = webRequest.GetRequestStream()) 
     writer.Write(postdata, 0, postdata.Length); 

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
    { 
     //We need to add any response cookies to our cookie container 
     cookies.Add(webResponse.Cookies); 

     //Only for debug 
     using (var stream = new StreamReader(webResponse.GetResponseStream())) 
      System.Diagnostics.Debug.WriteLine(stream.ReadToEnd()); 

     return (webResponse.StatusCode == HttpStatusCode.OK); 
    } 
} 

public string GETHtml(string url, CookieContainer cookies) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
    webRequest.CookieContainer = cookies; 
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;"; 
    webRequest.Referer = "http://www.purevolume.com/login"; 

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
    {  
     //We need to add any response cookies to our cookie container   
     cookies.Add(webResponse.Cookies); 

     using (var stream = new StreamReader(webResponse.GetResponseStream())) 
      return stream.ReadToEnd(); 
    } 
} 
+0

我試過,但網頁仍然登錄頁面上的StatusCode是真的... 我怎麼會知道提供的用戶名和密碼是否正確?我認爲響應應返回「http://www.purevolume.com/dashboard」(成功登錄後打開的頁面)的流... 因爲我是一個新的bie,所以我期待完美的運行代碼:( – xtremist 2010-07-29 07:00:22

+0

@xtremist:我沒有帳戶,所以我很難看到問題所在,但是您應該使用Fiddler或類似的工具。使用Fiddler您可以看到原始的HTTP請求,並且您應該使用它來查找使用普通瀏覽器與上面的代碼登錄時的區別 – 2010-07-29 08:01:51

+0

@xtremist:Debug.WriteLine輸出什麼?在那裏輸出響應文本 – 2010-07-29 08:06:17