目录: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 表示登陆成功了。
附件: