C# 百度最新模擬登入原始碼 RSA加密
阿新 • • 發佈:2019-02-02
因為要做百度網盤的相關操作,,需要登入百度,網上搜了半天都是以前百度沒用RSA加密時候的原始碼,,只能自己動手,說實話挺累的,花了一晚上時間終於可以了!
還有個新浪微博登入的,看了下網上能搜到,就不發了。希望對大家有用!
感謝蘇菲的httphelper類 省了不少時間!
//主要程式碼 //獲取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; }
下載原始碼