[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;
}