コード例 #1
0
        /// <summary>
        /// 循环询问验证码图片的答案
        /// </summary>
        private void LoopInquiryCaptchaAnswer()
        {
            while (isInquiryWork)
            {
                long ss = KK.CurrentMills();
                try
                {
                    var  taskContext = CaptchaTaskContext.me;
                    long s1          = KK.CurrentMills();
                    if (CaptchaTaskContext.me.IsAllImagesAnswered())
                    {
                        KK.Sleep(30);
                        continue;
                    }

                    var images = new List <CaptchaAnswerImage>(taskContext.GetImagesOfAwaitAnswer());

                    logger.DebugFormat("inquiry answer, image size is {0}. First Uuid is {1}", images.Count, images[0].Uuid);

                    foreach (var img in images)
                    {
                        if (img == null)
                        {
                            continue;
                        }

                        if (img.Answer == null || img.Answer.Length == 0)
                        {
                            var req = KK.CreateImageAnswerRequest(img.Uuid);

                            DataResult <CaptchaImageAnswerResponse> dr = HttpClients
                                                                         .PostAsJson <DataResult <CaptchaImageAnswerResponse> >(conf.GetCaptchaAnswerUrl, req);

                            if (DataResults.IsOK(dr) && dr.Data?.answer?.Length > 0)
                            {
                                img.Answer = dr.Data.answer;

                                taskContext.PutAnswer(img.Uuid, dr.Data.answer);
                                taskContext.RemoveAwaitImage(img.Uuid);

                                logger.InfoFormat("GET task#{0}'s answer is {1}", img.Uuid, dr.Data.answer);

                                // TODO:这段应该剥离出去
                                // TryInputAnswerAhead(img.Uuid, dr.Data.answer);
                                captchaInputCallbackFunc?.Invoke(img);
                            }
                        }
                        else
                        {
                            taskContext.RemoveAwaitImage(img.Uuid);
                            // captchaInputCallbackFunc?.Invoke(img);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Error("LoopInquiryCaptchaAnswer error:", e);
                }
                finally
                {
                    KK.Sleep(30);
                }
            }

            logger.InfoFormat("END LoopInquiryCaptchaAnswer.");
        }