如何以编程方式登录雅虎网站 [英] how to login to yahoo website programatically

查看:121
本文介绍了如何以编程方式登录雅虎网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

马上开始 - 请不要暗示我使用Yahoo API。我纯粹是以学习体验的方式来做这些事情,并且使用API​​会破坏目的。



我使用Fiddler在登录到Yahoo邮件时查看HTTP流量mail.yahoo.com)或Flickr。我发现浏览器会将数据发布到 https://login.yahoo.com/config/login 。示例发布数据是:


.tries = 1& .src = flickrsignin& .md5 =& .hash =& .js = &安培;。去年=安培;促销=安培; .intl =美国和放大器; .lang = EN-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& popup = 1& login = nkisnksd& passwd = noasno& .save =& passwd_raw =


这里有很多值,比如挑战字符串,我不知道浏览器是如何处理它的。我怎样才能弄清楚浏览器正在采取哪些步骤来提出挑战响应?我认为这是一个使用存储cookie的算法,当我获取页面时,但不知道浏览器是如何自动知道算法的?

谢谢!

解决方案

这是我成功使用的一种方法。本质上,您使用C#WebBrowser控件并导航到登录URL。然后,你循环遍历元素来查找登录名和密码字段(您需要查看页面源以查找其名称)。然后你模拟点击登录按钮。

  void browser_DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)
{
//加载Yahoo登录页面
if(browser.Url.AbsoluteUri.Contains(loginUrl))
{
if(browser.Document!= null)
{
//查找并填写username文本框
HtmlElementCollection collection = browser.Document.GetElementsByTagName(input);
foreach(集合中的HtmlElement元素)
{
string name = element.GetAttribute(id);
if(name ==username)
{
element.SetAttribute(value,_login);
休息;



//查找并填充密码字段
foreach(集合中的HtmlElement元素)
{
字符串名称= element.GetAttribute(id);
if(name ==passwd)
{
element.SetAttribute(value,_password);
休息;
}
}

//提交表单
collection = browser.Document.GetElementsByTagName(button);
foreach(集合中的HtmlElement元素)
{
string name = element.GetAttribute(id);
if(name ==.save)
{
element.InvokeMember(click);
休息;
}
}
}
}
}


Right off the bat - please do not suggest I use the Yahoo API. I am doing this purely as a learning experience and using the API would defeat the purpose.

I am using Fiddler to look at the HTTP traffic when I log into Yahoo mail (mail.yahoo.com) or Flickr. I see that the browser posts data to https://login.yahoo.com/config/login. Sample post data is:

.tries=1&.src=flickrsignin&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.lang=en-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&pad=15&aad=15&popup=1&login=nkisnksd&passwd=noasno&.save=&passwd_raw=

As you see, there are lots of values in there, such as the challenge string, which I don't know how the browser comes up with it. How can I figure out the steps the browser is taking to come up with the challenge response? I assume it's an algorithm using the cookies stored when I GET the page, but just not sure how the browser automatically knows the algorithm?

Thanks!

解决方案

Here is one approach that I used successfully. Essentially, you use the C# WebBrowser control and navigate to the login url. Then you loop through elements to find the login and password fields (you'll need to look at the page source to find out their names). Then you simulate the click on the login button.

    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;
                    }
                }
            }
        }
    }

这篇关于如何以编程方式登录雅虎网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆