zoukankan      html  css  js  c++  java
  • C#模拟百度登录并到指定网站评论回帖(四)

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

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

    内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回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;

    }

    }

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

  • 相关阅读:
    1_Selenium环境搭建
    python functools
    python 参数注解inspect
    python 堆排序
    python functools
    python 装饰器
    python 柯里化
    python 高阶函数
    python 树
    python 函数销毁
  • 原文地址:https://www.cnblogs.com/AssertionBird/p/4815987.html
Copyright © 2011-2022 走看看