Ejemplo n.º 1
0
        protected override Tuple <string, HttpWebRequest, HttpWebResponse> DoLoginWhenEnableVerificationCode(Tuple <string, HttpWebRequest, HttpWebResponse> cookieResult, bool isAutoOrc = true)
        {
            if (cookieResult == null)
            {
                cookieResult = HttpUtils.GetData(Site.Url, _cookie);
            }

            _cookie = cookieResult != null && cookieResult.Item2 != null ? cookieResult.Item2.CookieContainer : _cookie;
            Random ran    = new Random();
            string imgUrl = UrlUtils.CombileUrl(Site.Url, string.Format("captcha.php?sid={0}", ran.NextDouble()));
            OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs();

            args.VerificationCodeUrl = imgUrl;
            args.Cookie = _cookie;
            args.Site   = Site;


            string checkCodeKey = OnVerificationCode(args);

            if (checkCodeKey.IsNullOrEmptyOrWhiteSpace())
            {
                return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null));
            }


            //if (Site.IsLoginByMail)
            //U2是否有使用用户名登录的接口?
            string postData = string.Format("login_type=email&login_ajax=1&username={0}&password={1}&captcha={2}&ssl=yes", HttpUtility.UrlEncode(User.Mail), HttpUtility.UrlEncode(User.PassWord), checkCodeKey);

            return(HttpUtils.PostData(Site.LoginUrl, postData, _cookie));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 触发验证码事件
 /// </summary>
 /// <param name="e"></param>
 public virtual string OnVerificationCode(OnVerificationCodeEventArgs e)
 {
     if (VerificationCode != null)
     {
         return(VerificationCode.Invoke(this, e));
     }
     return(string.Empty);
 }
Ejemplo n.º 3
0
 public VerificationCodeFrm(OnVerificationCodeEventArgs e)
 {
     El = e;
     InitializeComponent();
     if (El != null)
     {
         if (!El.VerificationCodeUrl.IsNullOrEmptyOrWhiteSpace())
         {
             picCode.Image = FormUtils.ImageFromWebTest(El.VerificationCodeUrl);
         }
         if (El.Site != null)
         {
             this.Text = El.Site.Name + "验证码";
         }
     }
 }
Ejemplo n.º 4
0
        private string GetVerificationCode(string imgUrl, bool isAutoOrc = true)
        {
            string checkCodeKey = string.Empty;
            Bitmap bmp          = null;

            if (isAutoOrc)
            {
                try
                {
                    bmp = ImageUtils.GetOrcImage((Bitmap)ImageUtils.ImageFromWebTest(imgUrl, _cookie));
                    if (bmp != null)
                    {
                        var orcResults = BaiDuApiUtil.WebImage(bmp);
                        if (orcResults.Any())
                        {
                            checkCodeKey = orcResults.FirstOrDefault();
                            string regEx = @"[^a-z0-9]";
                            checkCodeKey = Regex.Replace(checkCodeKey, regEx, "", RegexOptions.IgnoreCase);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("{0} 验证码识别异常。异常原因:{1}", Site.Name, ex.GetInnerExceptionMessage()), ex);
                }
            }
            if (!isAutoOrc || checkCodeKey.Length < 6)
            {
                //目前Frds模板的验证码都是6位
                OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs();
                args.VerificationCodeUrl = imgUrl;
                args.Site    = Site;
                checkCodeKey = OnVerificationCode(args);
            }
            return(checkCodeKey);
        }
Ejemplo n.º 5
0
        private string Sign(bool isAuto, bool isAutoOrc, int count)
        {
            var result = HttpUtils.GetData(Site.SignUrl, _cookie);

            if (HttpUtils.IsErrorRequest(result.Item1))
            {
                return(result.Item1);
            }

            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(result.Item1);

            var imgNode = htmlDocument.DocumentNode.SelectSingleNode("//img");

            if (imgNode != null && imgNode.Attributes.Contains("src"))
            {
                string imgUrl = HttpUtility.HtmlDecode(imgNode.Attributes["src"].Value);
                if (imgUrl.IsNullOrEmptyOrWhiteSpace())
                {
                    return("无法获取到签到验证码。");
                }

                imgUrl = UrlUtils.CombileUrl(Site.Url, imgUrl);

                string checkCodeHash = imgUrl.UrlSearchKey("imagehash");
                string checkCodeKey  = string.Empty;
                Bitmap bmp           = null;
                if (isAutoOrc)
                {
                    try
                    {
                        bmp = ImageUtils.GetOrcImage((Bitmap)ImageUtils.ImageFromWebTest(imgUrl, _cookie));
                        if (bmp != null)
                        {
                            var orcResults = BaiDuApiUtil.WebImage(bmp);
                            if (orcResults.Any())
                            {
                                checkCodeKey = orcResults.FirstOrDefault();
                                string regEx = @"[^a-z0-9]";
                                checkCodeKey = Regex.Replace(checkCodeKey, regEx, "", RegexOptions.IgnoreCase);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(string.Format("{0} 验证码识别异常。异常原因:{1}", Site.Name, ex.GetInnerExceptionMessage()), ex);
                    }
                }
                if (!isAutoOrc)
                {
                    OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs();
                    args.VerificationCodeUrl = imgUrl;
                    args.Site    = Site;
                    checkCodeKey = OnVerificationCode(args);
                }

                string postData = string.Format("imagehash={0}&imagestring={1}", checkCodeHash, checkCodeKey);
                result = HttpUtils.PostData(Site.SignUrl + "?cmd=signin", postData, _cookie);
                if (HttpUtils.IsErrorRequest(result.Item1))
                {
                    return(result.Item1);
                }

                var    o       = JsonConvert.DeserializeObject(result.Item1) as JObject;
                string message = o["msg"].TryPareValue <string>();
                if (o["state"].TryPareValue <string>().EqualIgnoreCase("success"))
                {
                    string signDay = o["signindays"].TryPareValue <string>();
                    string bonus   = o["integral"].TryPareValue <string>();
                    return(string.Format("签到成功,连续天数:{0},积分:{1}", signDay, bonus));
                }
                else
                {
                    if (message.IsNullOrEmptyOrWhiteSpace())
                    {
                        return(string.Format("签到失败,失败原因:{0}", "你可能已经签过到了。"));
                    }
                }

                if (!isAuto && count <= 1)
                {
                    //如果签到失败且之前是自动签到
                    Logger.Info(string.Format("{0} 签到失败,识别到的验证码为{1}。", Site.Name, checkCodeKey));
                    return(Sign(isAuto, false, ++count));
                }
                else if (isAuto && count <= 2)
                {
                    return(Sign(isAuto, true, ++count));
                }
                else
                {
                    return(string.Format("签到失败,失败原因:{0}", message));
                }
            }
            else
            {
                return("签到失败,获取签到验证码失败。");
            }
        }
Ejemplo n.º 6
0
        public override string Login()
        {
            var          result       = HttpUtils.PostData(Site.Url, "", _cookie);
            var          htmlResult   = result.Item1;
            HtmlDocument htmlDocument = new HtmlDocument();

            htmlDocument.LoadHtml(htmlResult);//加载HTML字符串,如果是文件可以用htmlDocument.Load方法加载

            //这里用Cookie登录,有可能之前已经登录成功了,不会再次出现验证码
            if (htmlResult.Contains(User.UserName))
            {
                User.Id = GetUserId(htmlResult);
                return("登录成功。");
            }
            else
            {
                HtmlNode node          = htmlDocument.DocumentNode.SelectSingleNode("//*[@id=\"nav_block\"]/form[2]/table/tr[3]/td[2]/img");//跟Xpath一样
                string   checkCodeKey  = string.Empty;
                string   checkCodeHash = string.Empty;

                if (node != null)
                {
                    string imgUrl = HttpUtility.HtmlDecode(node.Attributes["src"].Value);
                    if (imgUrl.IsNullOrEmptyOrWhiteSpace())
                    {
                        return("无法获取到验证码,登录失败,请稍后重试。");
                    }

                    imgUrl = "https://pt.keepfrds.com/" + imgUrl;
                    OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs();
                    args.VerificationCodeUrl = imgUrl;
                    args.Site     = Site;
                    checkCodeKey  = OnVerificationCode(args);
                    checkCodeHash = imgUrl.UrlSearchKey("imagehash");

                    if (checkCodeKey.IsNullOrEmptyOrWhiteSpace() || checkCodeHash.IsNullOrEmptyOrWhiteSpace())
                    {
                        return("无法获取到验证码,登录失败,请稍后重试。");
                    }

                    string postData = string.Format("username={0}&password={1}&imagestring={2}&imagehash={3}&ssl=yes&trackerssl=yes", User.UserName, User.PassWord, checkCodeKey, checkCodeHash);
                    result     = HttpUtils.PostData(Site.LoginUrl, postData, _cookie);
                    htmlResult = result.Item1;
                    if (htmlResult.Contains(User.UserName))
                    {
                        User.Id = GetUserId(htmlResult);
                        _cookie = result.Item2.CookieContainer;
                        SetLocalCookie(_cookie);
                        return("登录成功。");
                    }
                    else
                    {
                        htmlDocument = new HtmlDocument();
                        htmlDocument.LoadHtml(htmlResult);                                                                   //加载HTML字符串,如果是文件可以用htmlDocument.Load方法加载
                        node = htmlDocument.DocumentNode.SelectSingleNode("//*[@id=\"nav_block\"]/table/tr/td/table/tr/td"); //跟Xpath一样
                        string errMsg = htmlResult;
                        if (node != null)
                        {
                            errMsg = node.InnerText;
                        }
                        return(string.Format("登录失败,失败原因:{0}", errMsg));
                    }
                }
                else
                {
                    return(string.Format("登录失败,失败原因:无法获取验证码,请重试。"));
                }
            }
        }
Ejemplo n.º 7
0
        private string Sign(bool isAuto, bool isAutoOrc, int count)
        {
            //获取ImageHash
            string hostUrl  = Site.Url;
            string hashUrl  = UrlUtils.CombileUrl(hostUrl, "/image_code_ajax.php");
            string postData = "action=new";
            var    result   = HttpUtils.PostData(hashUrl, postData, _cookie);

            if (HttpUtils.IsErrorRequest(result.Item1))
            {
                return(result.Item1);
            }
            JObject o = JsonConvert.DeserializeObject(result.Item1) as JObject;

            if (o["success"].TryPareValue <bool>())
            {
                string checkCodeHash = o["code"].TryPareValue <string>();
                string imgUrl        = UrlUtils.CombileUrl(hostUrl, string.Format("image.php?action=regimage&imagehash={0}", checkCodeHash));
                string checkCodeKey  = string.Empty;
                Bitmap bmp           = null;
                if (isAutoOrc)
                {
                    try
                    {
                        bmp = ImageUtils.GetOrcImage((Bitmap)ImageUtils.ImageFromWebTest(imgUrl, _cookie));
                        if (bmp != null)
                        {
                            var orcResults = BaiDuApiUtil.WebImage(bmp);
                            if (orcResults.Any())
                            {
                                checkCodeKey = orcResults.FirstOrDefault();
                                string regEx = @"[^a-z0-9]";
                                checkCodeKey = Regex.Replace(checkCodeKey, regEx, "", RegexOptions.IgnoreCase);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(string.Format("{0} 验证码识别异常。异常原因:{1}", Site.Name, ex.GetInnerExceptionMessage()), ex);
                    }
                }
                if (!isAutoOrc)
                {
                    OnVerificationCodeEventArgs args = new OnVerificationCodeEventArgs();
                    args.VerificationCodeUrl = imgUrl;
                    args.Site    = Site;
                    checkCodeKey = OnVerificationCode(args);
                }

                postData = string.Format("action=showup&imagehash={0}&imagestring={1}", checkCodeHash, checkCodeKey);
                result   = HttpUtils.PostData(Site.SignUrl, postData, _cookie);
                if (HttpUtils.IsErrorRequest(result.Item1))
                {
                    return(result.Item1);
                }

                o = JsonConvert.DeserializeObject(result.Item1) as JObject;
                string message = o["message"].TryPareValue <string>();
                if (o["success"].TryPareValue <bool>())
                {
                    return(string.Format("签到成功,积分:{0}", message));
                }

                if (message.EqualIgnoreCase("date_unmatch"))
                {
                    return(string.Format("签到失败,失败原因:{0}", message));
                }

                if (!isAuto && count <= 1)
                {
                    //如果签到失败且之前是自动签到
                    Logger.Info(string.Format("{0} 签到失败,识别到的验证码为{1}。", Site.Name, checkCodeKey));
                    return(Sign(isAuto, false, ++count));
                }
                else if (isAuto && count <= 2)
                {
                    return(Sign(isAuto, true, ++count));
                }
                else
                {
                    return(string.Format("签到失败,失败原因:{0}", message));
                }
            }
            else
            {
                return("签到失败,获取签到验证码失败。");
            }
        }