2012-02-03 99 views
1

馬上關閉 - 請不要暗示我使用Yahoo API。我純粹是以學習體驗的方式來做這件事,而使用API​​會挫敗目的。如何以編程方式登錄到雅虎網站

我使用Fiddler在登錄到雅虎郵件(mail.yahoo.com)或Flickr時查看HTTP流量。我看到瀏覽器發佈數據到https://login.yahoo.com/config/login。採樣後的數據是:

.tries = 1 &的.src = flickrsignin & .md5 = & .hash = &的.js = &。去年= &促銷= & .intl =我們& .lang =恩US & .bypass = & .partner = & .U = 811cdp17imj21 & .V = 0 & .challenge = iwQ4dJLk0KhUP8Xlpyji_8ftQ.fe & .yplus = & .emailCode = &的pkg = & stepid = & .ev = & hasMsgr = 1 & .chkP = Y & .done = https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickrsignin%26.pc%3D8190%26.scrumb%3D0%26.pd %3Dc%253DJvVF95K62e6PzdPu7MBv2V8-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fphotos%25252Ffriends%25252F & .pd = flickrsignin_ver%3D0 %26C%3DJvVF95K62e6PzdPu7MBv2V8-%26ivt%3D%26sg%3D & .WS = 1 & .cp = 0 &墊= 15 & AAD = 15 &彈出= 1 &登錄名= nkisnksd &的passwd = noasno & .save = & passwd_raw =

正如你所看到的,那裏有很多值,比如挑戰字符串,我不知道瀏覽器是如何提供的。我怎樣才能弄清楚瀏覽器正在採取哪些步驟來提出挑戰響應?我假設這是一個使用存儲cookie的算法,當我獲取頁面時,但不確定瀏覽器如何自動知道算法?

謝謝!

+2

看看在y!登錄頁面源代碼。它將包含許多有用的花絮,以幫助您找出其他值。網站來源幾乎總是會披露關於如何執行特定網頁所做的事情的信息。 – 2012-02-03 03:12:45

+0

看看那個......你是對的挑戰反應字符串是正確的源代碼與登錄表單...現在我猜我只需要使用302 URL作爲引用和挑戰字符串(和其他查詢值)。謝謝!! – blizz 2012-02-03 03:48:04

+1

我從來沒有這樣做過!但它幾乎總是。 :) – 2012-02-03 03:49:12

回答

1

以下是我成功使用的一種方法。本質上,您使用C#WebBrowser控件並導航到登錄URL。然後,你循環遍歷元素來查找登錄名和密碼字段(您需要查看頁面源以查找其名稱)。然後你模擬點擊登錄按鈕。

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     //loaded the Yahoo login page 
     if (browser.Url.AbsoluteUri.Contains(loginUrl)) 
     { 
      if (browser.Document != null) 
      { 
       //Find and fill the "username" textbox 
       HtmlElementCollection collection = browser.Document.GetElementsByTagName("input"); 
       foreach (HtmlElement element in collection) 
       { 
        string name = element.GetAttribute("id"); 
        if (name == "username") 
        { 
         element.SetAttribute("value", _login); 
         break; 
        } 
       } 

       //Find and fill the "password" field 
       foreach (HtmlElement element in collection) 
       { 
        string name = element.GetAttribute("id"); 
        if (name == "passwd") 
        { 
         element.SetAttribute("value", _password); 
         break; 
        } 
       } 

       //Submit the form 
       collection = browser.Document.GetElementsByTagName("button"); 
       foreach (HtmlElement element in collection) 
       { 
        string name = element.GetAttribute("id"); 
        if (name == ".save") 
        { 
         element.InvokeMember("click"); 
         break; 
        } 
       } 
      } 
     } 
    } 
0

這是另一種方法,也工作。我在滾動!

 CookieContainer _yahooContainer; 
     string _login = "myyahoologin"; 
     string _password = "myyahoopassword"; 

     string strPostData = String.Format("login={0}&passwd={1}", _login, _password); 

     // Setup the http request. 
     HttpWebRequest wrWebRequest = WebRequest.Create(LoginUrl) as HttpWebRequest; 
     wrWebRequest.Method = "POST"; 
     wrWebRequest.ContentLength = strPostData.Length; 
     wrWebRequest.ContentType = "application/x-www-form-urlencoded"; 
     _yahooContainer = new CookieContainer(); 
     wrWebRequest.CookieContainer = _yahooContainer; 

     // Post to the login form. 
     using (StreamWriter swRequestWriter = new StreamWriter(wrWebRequest.GetRequestStream())) 
     { 
      swRequestWriter.Write(strPostData); 
      swRequestWriter.Close();   
     } 

     // Get the response. 
     HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse(); 

     if (hwrWebResponse.ResponseUri.AbsoluteUri.Contains("my.yahoo.com")) 
     { 
      // you authenticated properly 
     } 

     // Now use the cookies to create more requests. 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_downloadUrl); 
     req.CookieContainer = _yahooContainer; 
     HttpWebResponse resp = (HttpWebResponse)req.GetResponse();