|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ApiWinHttp;
- namespace baidu
- {
- class LoginBaidu
- {
- WinInetHttp inetHttp;
- TimeHelper time;
- private string _Url { get; set; }
- public LoginBaidu()
- {
- inetHttp = new WinInetHttp();
- time = new TimeHelper();
- }
- /// <summary>
- /// 获取 callback 值
- /// </summary>
- /// <returns></returns>
- public string Getcallback()
- {
- JavaScript myjs = new JavaScript(ScriptLanguage1.JavaScript);
- return myjs.Eval("fuckall()", " function fuckall() { return Math.floor(Math.random() * 2147483648).toString(36); }");
- }
- /// <summary>
- /// 访问网页 并返回网页源码 失败返回空
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public string Login(string url)
- {
- if( inetHttp.HttpRequest(url) != "true")
- {
- return "";
- }
- return inetHttp.GetHtml();
- }
- /// <summary>
- /// 获取头部信息
- /// </summary>
- /// <returns></returns>
- public string GetHeader()
- {
- return inetHttp.GetHttpQueryInfo();
- }
- public string _token { get; set; }
- /// <summary>
- /// 获取 token 值
- /// </summary>
- /// <returns></returns>
- public string GetTokenValue()
- {
- string url = "https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=" + time.GetExistingTime() + "&class=login&logintype=dialogLogin&callback=bd__cbs__" + this.Getcallback() + "";
- string res = this.Login(url);
- if (string.IsNullOrWhiteSpace(res))
- {
- return "";
- }
- string t = Stringoperation.Between(res, "(", ")");
- Newtonsoft.Json.Linq.JObject jobe = Newtonsoft.Json.Linq.JObject.Parse(t);
- string jo = jobe["data"]["token"].ToString(); //Songs字符串下 的数组中 第0个 SongLength 的值
- if (string.IsNullOrWhiteSpace(jo))
- {
- return "";
- }
- return jo;
- }
- /// <summary>
- /// 通过 token 值 获取 UBI cookie
- /// </summary>
- /// <returns>返回 0 就是没出错,否则出错</returns>
- public string GetCookieUBI()
- {
- string url = "https://passport.baidu.com/v2/api/?loginhistory&token=" + _token + "&tpl=mn&apiver=v3&tt=" + time.GetExistingTime() + "&callback=bd__cbs__" + this.Getcallback() + "";
- string res = this.Login(url);
- if (string.IsNullOrWhiteSpace(res))
- {
- return "";
- }
- string t = Stringoperation.Between(res, "(", ")");
- Newtonsoft.Json.Linq.JObject jobe = Newtonsoft.Json.Linq.JObject.Parse(t);
- string jo = jobe["errInfo"]["no"].ToString(); //Songs字符串下 的数组中 第0个 SongLength 的值
- if (string.IsNullOrWhiteSpace(jo))
- {
- return "";
- }
- return jo;
- }
- /// <summary>
- /// 判断是否需要验证码,是在离开用户名框,发送的
- /// </summary>
- /// <returns>返回 “false” 表示不需要验证码 ,"true" 表示需要验证码,返回其他表示出错了。。</returns>
- public string IsCodeString(out string CondeString,string user)
- {
- CondeString = string.Empty;
- string url = "https://passport.baidu.com/v2/api/?logincheck&token=" + _token + "&tpl=mn&apiver=v3&tt=" + time.GetExistingTime() + "&username=" + user + "&isphone=false&callback=bd__cbs__" + this.Getcallback() + "";
- string res = this.Login(url);
- if (string.IsNullOrWhiteSpace(res))
- {
- return "访问判断验证码失败!!!!!!!!!!!!!!!";
- }
- string t = Stringoperation.Between(res, "(", ")");
- Newtonsoft.Json.Linq.JObject jobe = Newtonsoft.Json.Linq.JObject.Parse(t);
- string jo = jobe["errInfo"]["no"].ToString();
- if (string.IsNullOrWhiteSpace(jo)) //不等于空,就表示,获取到网页源码
- {
- return "访问出错!!!!!!!!!";
- }
- if (jo == "0") //== 0 表示 网页没有出错
- {
- jobe = Newtonsoft.Json.Linq.JObject.Parse(t);
- jo = jobe["data"]["codeString"].ToString();
- if (string.IsNullOrWhiteSpace(jo)) //验证码为空就表示 不需要验证码
- {
- return "false"; // false 表示不需要验证码
- }
- string codeString = jobe["data"]["codeString"].ToString();
- if (string.IsNullOrWhiteSpace(codeString))
- {
- return "验证码参数为空!!!!!!";
- }
- CondeString = codeString + "|" + jobe["data"]["vcodetype"].ToString();
- return "true";
- }
- return "出错了!!!!!!!!!";
- }
- /// <summary>
- /// 获取 验证码 图片
- /// </summary>
- /// <param name="codestring"></param>
- public System.Drawing.Image GetCodeImg(string codestring)
- {
- string url = "https://passport.baidu.com/cgi-bin/genimage?" + codestring + "";
- string res = this.Login(url);
- if (string.IsNullOrWhiteSpace(res))
- {
- return null;
- }
- return inetHttp.GetHtmlImage();
- }
- /// <summary>
- /// POST登录 失败返回 "false"
- /// </summary>
- /// <param name="user">用户名</param>
- /// <param name="passwrd">密码</param>
- /// <param name="codestring">验证码参数 默认为空</param>
- /// <param name="Vcode">验证码 默认为空</param>
- /// <returns></returns>
- public string PostLogin(string user,string passwrd,string codestring = "",string Vcode="")
- {
- string url = "https://passport.baidu.com/v2/api/?login";
- //inetHttp.SetRequestHeaders("Referer", "http://www.baidu.com/");
- string postData = "staticpage=http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fv3Jump.html&charset=utf-8&token="+_token+"&tpl=mn&apiver=v3&tt="+time.GetExistingTime()+"&codestring="+codestring+"&safeflg=0&u=http%3A%2F%2Fwww.baidu.com%2F&isPhone=false&quick_user=0&loginmerge=true&logintype=dialogLogin&splogin=rate&username="+user+"&password="+passwrd+"&verifycode="+Vcode+"&mem_pass=on&ppui_logintime=7053&callback=parent.bd__pcbs__"+this.Getcallback()+"";
- string res = inetHttp.HttpRequest(url, "POST", "", postData);
- if (res != "true")
- {
- return res + "false";
- }
- return inetHttp.GetHttpQueryInfo();
- }
- public string GetHtml()
- {
- return inetHttp.GetHtml();
- }
- }
- }
复制代码 |
|