http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 10077|回复: 12

[例子] 百度最新登录源码(2015-06-01)-儿童节福利

[复制链接]
发表于 2015-6-1 22:06:10 | 显示全部楼层 |阅读模式
本帖最后由 crazylin 于 2015-6-1 23:38 编辑

因为要做百度网盘的相关操作,,需要登录百度,网上搜了半天都是以前百度没用RSA加密时候的源码,,只能自己动手,说实话挺累的,花了一晚上时间终于可以了!
还有个新浪微博登录的,看了下网上能搜到,就不发了。希望对大家有用!
感谢苏菲的httphelper类  省了不少时间!

QQ图片20150601220445.png
[C#] 纯文本查看 复制代码
        //主要代码

        //获取token
        public bool GetToken()
        {
            var url = "https://passport.baidu.com/v2/api/?getapi&";

            var nvc = new NameValueCollection
            {
                {"apiver", "v3"},
                {"callback", "customName"},
                {"class", ""},
                {"logintype", "basicLogin"},
                {"tpl", "pp"},
                {"tt", DateTime.Now.Ticks.ToString()},
            };
            var httpResult = new HttpHelper().GetHtml(
                new HttpItem()
                {
                    URL = url + HttpHelper.DataToString(nvc),
                    Method = "GET",
                    CookieContainer = cookies,
                    ResultCookieType = ResultCookieType.CookieContainer
                });
            if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
            {
                var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));
                if (mc.Success)
                {
                    dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);
                    var no = obj.errInfo.no.Value;
                    if (no.Equals("0"))
                    {
                        token = obj.data.token.Value;
                        return true;
                    }
                }

            }
            return false;
        }
        //获取key
        public bool GetPubksyAndRsakey()
        {
            var url = "https://passport.baidu.com/v2/getpublickey?";
            var nvc = new NameValueCollection
            {
                {"apiver", "v3"},
                {"callback", "customName"},
                {"token", token},
                {"tpl", "pp"},
                {"tt", DateTime.Now.Ticks.ToString()},
            };
            var httpResult = new HttpHelper().GetHtml(
                new HttpItem()
                {
                    URL = url + HttpHelper.DataToString(nvc),
                    Method = "GET",
                    CookieContainer = cookies,
                    ResultCookieType = ResultCookieType.CookieContainer
                });
            if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
            {
                var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html);
                if (mc.Success)
                {
                    dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);
                    var no = obj.errno.Value;
                    if (no.Equals("0"))
                    {
                        pubksy = obj.pubkey.Value;
                        rsakey = obj.key.Value;
                        return true;
                    }
                }
                return true;
            }
            return false;
        }
        //获取验证码
        public Image GetValCode()
        {
            var url = "https://passport.baidu.com/v2/?reggetcodestr&";
            var nvc = new NameValueCollection
            {
                {"apiver", "v3"},
                {"callback", "customName"},
                {"fr", "login"},
                {"token", token},
                {"tpl", "pp"},
                {"tt", DateTime.Now.Ticks.ToString()},
            };
            var httpResult = new HttpHelper().GetHtml(
                new HttpItem()
                {
                    URL = url + HttpHelper.DataToString(nvc),
                    Method = "GET",
                    CookieContainer = cookies,
                    ResultCookieType = ResultCookieType.CookieContainer
                });
            if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
            {
                var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));
                if (mc.Success)
                {
                    dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);
                    var no = obj.errInfo.no.Value;
                    if (no.Equals("0"))
                    {
                        verifyStr = obj.data.verifyStr.Value;
                        var verifySign = obj.data.verifySign.Value;
                        url = "https://passport.baidu.com/cgi-bin/genimage?";
                        httpResult = new HttpHelper().GetHtml(
                            new HttpItem()
                            {
                                URL = url + verifyStr,
                                Method = "GET",
                                CookieContainer = cookies,
                                ResultCookieType = ResultCookieType.CookieContainer,
                                ResultType = ResultType.Byte
                            });
                        if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
                        {
                            return Image.FromStream(new MemoryStream(httpResult.ResultByte));
                        }
                    }

                }
            }
            return null;
        }
        //检查兼验证码
        public bool CheckValCode(string code)
        {
            var url = "https://passport.baidu.com/v2/?checkvcode&";
            var nvc = new NameValueCollection
            {
                {"apiver", "v3"},
                {"token", token},
                {"tpl", "pp"},
                {"fr", "login"},
                {"token", token},
                {"tpl", "pp"},
                {"tt", DateTime.Now.Ticks.ToString()},
                {"verifycode", code},
                {"codestring", verifyStr},
                {"callback", "customName"},
            };
            var httpResult = new HttpHelper().GetHtml(
                new HttpItem()
                {
                    URL = url + HttpHelper.DataToString(nvc),
                    Method = "GET",
                    CookieContainer = cookies,
                    ResultCookieType = ResultCookieType.CookieContainer
                });
            if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
            {
                var mc = new Regex(@"\((?<jsonStr>.*)\)").Match(httpResult.Html.Replace("\n", string.Empty));
                if (mc.Success)
                {
                    dynamic obj = JsonConvert.DeserializeObject<JObject>(mc.Groups["jsonStr"].Value);
                    var no = obj.errInfo.no.Value;
                    if (no.Equals("0"))
                    {
                        valcode = code;
                        return true;
                    }
                }
            }
            return false;
        }
        //登录
        public bool Login(string userName, string pswd)
        {
            pswd = GetPswd(pswd, pubksy.Replace("\n", "\\n"));
            var url = "https://passport.baidu.com/v2/api/?login";
            var nvc = new NameValueCollection
            {
                {"apiver", "v3"},
                {"staticpage", "https://passport.baidu.com/static/passpc-account/html/v3Jump.html"},
                {"charset", "UTF-8"},
                {"token", token},
                {"tpl", "pp"},
                {"tt", DateTime.Now.Ticks.ToString()},
                {"codestring", verifyStr},
                {"safeflg", "0"},
                {"u", "https://passport.baidu.com/"},
                {"isPhone", "false"},
                {"detect", "1"},
                {"quick_user", "0"},
                {"logintype", "basicLogin"},
                {"logLoginType", "pc_loginBasic"},
                {"loginmerge", "true"},
                {"username", userName},
                {"password", pswd},
                {"verifycode", valcode},
                {"mem_pass", "on"},
                {"rsakey", rsakey},
                {"crypttype", "12"},
                {"ppui_logintime", "111111"},
                {"callback", "customName"},
            };
            var httpResult = new HttpHelper().GetHtml(
                new HttpItem
                {
                    URL = url,
                    Method = "POST",
                    ContentType = "application/x-www-form-urlencoded",
                    Postdata = HttpHelper.DataToString(nvc),
                    PostDataType = PostDataType.String,
                    CookieContainer = cookies,
                    ResultCookieType = ResultCookieType.CookieContainer
                });
            if (httpResult.StatusCode.Equals(HttpStatusCode.OK))
            {
                Debug.WriteLine(httpResult.Html);
                Debug.WriteLine(HttpHelper.GetAllCookiesToString(cookies));
                return HttpHelper.GetAllCookies(cookies).FirstOrDefault(c => c.Name.Equals("STOKEN")) != null;
            }
            return false;
        }


下载地址: Baidu.zip (540.91 KB, 下载次数: 331)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-6-1 23:14:10 | 显示全部楼层
可以的啊,新浪微博加密算法也不难找,也是RSA , 百度会调。 新浪应该没问题,
发表于 2015-6-2 09:43:01 | 显示全部楼层
谢谢啊~~~有分析过程么,  有些参数真不知道怎么来的。
发表于 2015-6-2 10:18:25 | 显示全部楼层
请教一下LZ
我从
https://passport.baidu.com/v2/?login 入手,为什么都没看见你baidu init函数的那几个URL
望指点~
 楼主| 发表于 2015-6-2 11:00:58 | 显示全部楼层
redcat 发表于 2015-6-2 10:18
请教一下LZ
我从
https://passport.baidu.com/v2/?login 入手,为什么都没看见你baidu init函数的那几个 ...

https://passport.baidu.com/v2/api/?getapi  这个打开页面就有  我用火狐的firebug可以看到
https://passport.baidu.com/v2/getpublickey 密码框获取焦点以后就会请求
发表于 2015-6-2 11:33:43 | 显示全部楼层
https://passport.baidu.com/v2/api/?getapi
我这里返回的是

var bdPass=bdPass||{};
bdPass.api=bdPass.api||{};
bdPass.api.params=bdPass.api.params||{};
bdPass.api.params._token='df18f8625f36f70957784a77d341147c';

                document.write('<script type="text/javascript" charset="UTF-8" src="https://passport.baidu.com/js/pass_api_.js?v=20131115"></script>');

貌似并不是一个登陆页面啊。
 楼主| 发表于 2015-6-2 12:27:28 | 显示全部楼层
redcat 发表于 2015-6-2 11:33
https://passport.baidu.com/v2/api/?getapi
我这里返回的是

默认返回是你那个   应该加上参数  apiver=v3 和 callback=XXXX   以jsonp的方式请求 返回的就是我那个
发表于 2015-6-2 18:33:23 | 显示全部楼层
crazylin 发表于 2015-6-2 12:27
默认返回是你那个   应该加上参数  apiver=v3 和 callback=XXXX   以jsonp的方式请求 返回的就是我那个

哦  这样,明白您的意思了~~~
那我请教下 这个地址您从哪调试出来的,因为我在百度的登陆页面 貌似没分析到这个请求啊。
 楼主| 发表于 2015-6-2 20:01:30 | 显示全部楼层
redcat 发表于 2015-6-2 18:33
哦  这样,明白您的意思了~~~
那我请教下 这个地址您从哪调试出来的,因为我在百度的登陆页面 貌似没分 ...

用firefox、chrome、ie11 都是这个请求 不知你是什么浏览器了。。
发表于 2015-6-3 11:49:52 | 显示全部楼层
crazylin 发表于 2015-6-2 20:01
用firefox、chrome、ie11 都是这个请求 不知你是什么浏览器了。。

我用的chrome~~~难道我操作不对?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-25 09:07

© 2014-2021

快速回复 返回顶部 返回列表