目錄:api
一、fiddler解析百度登陸地址服務器
二、處理傳入參數cookie
因工做須要,因此研究了下百度的登錄。首先打開https://passport.baidu.com/v2/?login,咱們用fiddler很快就能找到百度的登陸入口https://passport.baidu.com/v2/api/?login .以下圖:ide
在登陸入口https://passport.baidu.com/v2/api/?login 以前,百度先會去獲取publickey和token。token是服務器和客戶端關聯的惟一id。publickey是一個rsa(非對稱加密)用來加密輸入的密碼的。因此要模擬登陸。必需要拿到這兩個參數。加密
經過fildder咱們很快拿到了獲取token和publickey的地址。spa
token(https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&tt=1433836782422&class=login&logintype=basicLogin&callback=bd__cbs__7fwpot).net
publickey(https://passport.baidu.com/v2/api/?loginhistory&token=414cf195652963982d479ecf0cee814b&tpl=pp&apiver=v3&tt=1433836782658&callback=bd__cbs__57q1jk)code
能夠看出先拿到token,而後用這個token再去拿publickey。以下圖:orm
這兩個都拿到了。xml
注意:咱們看到返回的pubkey是以 -----BEGIN PUBLIC KEY----- 開始 和-----END PUBLIC KEY----- 結束的。這是pem格式。咱們要轉換成xml格式的。由於.net平臺自帶的RSACryptoServiceProvider解析的是xml字符串。因此有了下面的幫助類:
須要引用:BouncyCastle.Crypto.dll
public class RSAHelper { public static string PemToXml(string pem) { if (pem.StartsWith("-----BEGIN RSA PRIVATE KEY-----") || pem.StartsWith("-----BEGIN PRIVATE KEY-----")) { return GetXmlRsaKey(pem, obj => { if ((obj as RsaPrivateCrtKeyParameters) != null) return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)obj); var keyPair = (AsymmetricCipherKeyPair)obj; return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)keyPair.Private); }, rsa => rsa.ToXmlString(true)); } if (pem.StartsWith("-----BEGIN PUBLIC KEY-----")) { return GetXmlRsaKey(pem, obj => { var publicKey = (RsaKeyParameters)obj; return DotNetUtilities.ToRSA(publicKey); }, rsa => rsa.ToXmlString(false)); } throw new InvalidKeyException("Unsupported PEM format..."); } private static string GetXmlRsaKey(string pem, Func<object, RSA> getRsa, Func<RSA, string> getKey) { using (var ms = new MemoryStream()) using (var sw = new StreamWriter(ms)) using (var sr = new StreamReader(ms)) { sw.Write(pem); sw.Flush(); ms.Position = 0; var pr = new PemReader(sr); object keyPair = pr.ReadObject(); using (RSA rsa = getRsa(keyPair)) { var xml = getKey(rsa); return xml; } } } /// <summary> /// RSA加密 /// </summary> /// <param name="publickey"></param> /// <param name="content"></param> /// <returns></returns> public static string RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(publickey); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey"></param> /// <param name="content"></param> /// <returns></returns> public static string RSADecrypt(string privatekey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); } }
下面就能夠用HttpWebRequest開始模擬登陸了。當cookies中包含 BAIDUID 則說明登陸成功。還有就是訪問https://passport.baidu.com/v2/api/?login,返回的字符串中 err_no=0 表示登陸成功了。
附件: