0

因此,该网站需要填写验证码才能登录。

我正在使用 C# winforms 和 Selenium Webdrivers 以及 2Captcha 的 API

这是我将请求发送到 2Captcha 的代码

    public string SendCaptcha(string APIKey,string GoogleKey,string PageURL)
    {
        #region Captcha Solve
        try
        {
            #region POST
            System.Net.ServicePointManager.Expect100Continue = false;
            var request = (HttpWebRequest)WebRequest.Create("http://2captcha.com/in.php");

            var postData = "key="+APIKey+"&method=userrecaptcha&googlekey="+GoogleKey+"&pageurl="+PageURL; 
            var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            #endregion

            #region GET
            if (responseString.Contains("OK|"))
            {
                string results = responseString.Replace("OK|", "");
                return results;
            }
            else
            {
                return "Error";
            }
            #endregion
        }
        catch (Exception e)
        {
            string tt = e.Message;
            return tt;
        }
        #endregion
    }

这是我从 2Captcha 获取已解决的验证码 ID 的代码

    public string GetAnswer(string result,string APIKey)
    {
        #region Captcha Solve
        try
        {
            #region POST
            System.Net.ServicePointManager.Expect100Continue = false;
            var request = (HttpWebRequest)WebRequest.Create("http://2captcha.com/res.php");

            var postData = "key="+APIKey+"&action=get&id="+result;
            var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            #endregion

            #region GET
            if (responseString.Contains("OK|"))
            {
                string results = responseString.Replace("OK|", "");
                return results;
            }
            else
            {
                return "Error";
            }
            #endregion
        }
        catch (Exception e)
        {
            string tt = e.Message;
            return tt;
        }
        #endregion
    }

这是登录/验证码

EHtvdrive.Url = "http://earnhoney.com";
EHtvdrive.FindElementByXPath("//*[@id='regorlogdiv']").Click();
Thread.Sleep(5000);
EHtvdrive.FindElementByXPath("//*[@id='email']").SendKeys(textBox4.Text);
EHtvdrive.FindElementByXPath("//*[@id='psswrd']").SendKeys(textBox3.Text);
label16.Invoke((MethodInvoker)delegate { label16.Text = "Solving Captcha."; });
label16.Invoke((MethodInvoker)delegate { label16.ForeColor = Color.Orange; });
string datakey = EHtvdrive.FindElementByXPath("//[@id='reCAPReg']").GetAttribute("data-sitekey");
string result = SendCaptcha(textBox5.Text, datakey, "http://www.earnhoney.com/en");
Thread.Sleep(60000);
string answer = GetAnswer(result, textBox5.Text);
IJavaScriptExecutor js = (IJavaScriptExecutor)EHtvdrive;
string title = (string)js.ExecuteScript("document.getElementById('g-recaptcha-response').innerHTML='" + answer + "';");
//MessageBox.Show(answer);
Thread.Sleep(3000);
EHtvdrive.FindElementById("loginbtn").Click();
Thread.Sleep(3000);

所以问题是它在 GetAnswer 上给了我错误返回。这里有什么帮助吗?我知道这很复杂,但真的可以在这方面使用一些帮助。

4

0 回答 0