C#模拟百度登录并到指定网站评论回帖(四)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體:   |    |    |  超大  

基本的实现功能前面已经全部讲完,基本上可以复制黏贴完成登录百度的过程了

今天的这一贴就说说怎么获取百度的验证码

内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回2个值吗?是的,就是codeType和codeString这两个值,用前面的JSON数据解析可以分别获取。

前面也说到codeType是不变的(至少同一次请求时不变)。请求验证码的时候会要求传递这个参数,所以要想得到正确的验证码,这个参数是必不可少的,否则你请求多少次都是一个错误的验证码!

具体代码入下:

//封装更换验证码

public void ReGetCode()

{

string reSet_code = string.Format("https://passport.baidu.com/v2/?reggetcodestr&token={0}&tpl=zongheng&apiver=v3&tt={1}&fr=login&vcodetype={2}&callback=bd__cbs__ta1xss", token, Utility.GetTimeStamp(),vcodeType);

string codeContent = helper.GetPageResponse_Get(reSet_code, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F"),"*/*");

if (_regex.IsMatch(codeContent))

codeContent = _regex.Match(codeContent).Value;

var codeResult = JsonConvert.DeserializeObject<Reget_Code>(codeContent);

_codestring = codeResult.Data_Code.VerifyStr;

string url_NewCode = string.Format("https://passport.baidu.com/cgi-bin/genimage?{0}", _codestring);

Bitmap bp= helper.GetCode(url_NewCode, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F"));

pictureBox1.Image = bp;

}

可能这样单独看,有人会看不懂什么意思,这里就注释一下

reSet_code 是代表获取codestring的网页,后面的参数分别是前面获取到的token、生成的时间戳、以及codeType

codeContent 代表请求网页后返回的内容,参数就是用到前面的HTTPHelper类的Post方法需要提供的参数

接下来是分析内容,获取codestring,然后对验证码的图片网页重新发起请求,将验证码显示回picturebox上

至于codeType怎么来的,这个在请求登录的时候,发生异常就会返回

下面是检验验证码是否正确的方法,在文本框输入了验证码之后按回车

PRivate void txtcode_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Enter)

{

Thread th_2 = null;

if (e.KeyCode == Keys.Enter)

{

th_2 = new Thread(() =>

{

verifycode = txtcode.Text;

//先访问一次验证码校验页面,看看验证码是否正确

string url_checkCode = string.Format("https://passport.baidu.com/v2/?checkvcode&token={0}&tpl=zongheng&apiver=v3&tt={1}&verifycode={2}&codestring={3}&callback=bd__cbs__nm74kc", token, Utility.GetTimeStamp(), verifycode, _codestring);

string checkcode = helper.GetPageResponse_Get(url_checkCode, Utility.UrlDecode("http://passport.zongheng.com/?location=http%3A%2F%2Fwww.zongheng.com%2F"), "*/*");

if (_regex.IsMatch(checkcode))

checkcode = _regex.Match(checkcode).Value;

var code_msg = JsonConvert.DeserializeObject<CheckvCode>(checkcode);

CodeMsg(code_msg.ErrInfo.No);

if (code_msg.ErrInfo.No != "0")

return;

LoginStatus status = Login(verifycode, _codestring, token, raskey, userIndex);

GoComment_Page(status);

th_2.Abort();

});

th_2.Start();

th_2.IsBackground = true;

}

}

好了,今天就到这里,如果有问题,欢迎评论交流~

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航