2012-02-03 80 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();