如何以编程方式登录雅虎网站

马上就好 – 请不要建议我使用Yahoo API。 我这样做纯粹是一种学习经验,使用API​​会破坏目的。

当我登录Yahoo邮件(mail.yahoo.com)或Flickr时,我正在使用Fiddler来查看HTTP流量。 我看到浏览器将数据发布到https://login.yahoo.com/config/login 。 样本post数据是:

.tries = 1的.src = flickrsignin&.md5 =&哈希=&JS =。最后=促销= INTL。= US&.lang = EN-US&.bypass =&合作伙伴=&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的算法,但是不确定浏览器如何自动知道算法?

谢谢!

这是我成功使用的一种方法。 基本上,您使用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; } } } } } 

这是另一种方法也有效。 我在滚!

  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();