The code in C# for integrating 2captcha service.
See the Python code here with more explanation on 2captcha service using.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
using System.IO; using System; using System.Net; using System.Text; class Recaptcha2captcha { private static string captcha_service_key; private string site_key; private string page_url; public void setServiceKey(string service_key) { Recaptcha2captcha.captcha_service_key = service_key; } public void setSiteKey(string site_key) { this.site_key = site_key; } public void setPageUrl(string page_url) { this.page_url = page_url; } public string SendRequest() // HTTP POST { try { System.Net.ServicePointManager.Expect100Continue = false; var request = (HttpWebRequest)WebRequest.Create("http://2captcha.com/in.php"); var postData = "key="+ Recaptcha2captcha.captcha_service_key +"&method=userrecaptcha&googlekey=" + this.site_key + "&page_url=" + this.page_url; 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(); response.Close(); if (responseString.Contains("OK|")) { return responseString; } else { return "2captcha service return error. Error code:" + responseString; } } catch (Exception e) { return e.Message; } } public string SubmitForm(string RecaptchaResponseToken) // HTTP POST { // var page_url = "http://testing-ground.scraping.pro/recaptcha"; try { System.Net.ServicePointManager.Expect100Continue = false; var request = (HttpWebRequest)WebRequest.Create(this.page_url); var postData = "submit=submin&g-recaptcha-response=" + RecaptchaResponseToken; 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(); response.Close(); return responseString; } catch (Exception e) { return e.Message; } } // the request to retrieve g-recaptcha-response token from 2captcha service public string getToken(string captcha_id) // HTTP GET { WebClient webClient = new WebClient(); webClient.QueryString.Add("key", Recaptcha2captcha.captcha_service_key); webClient.QueryString.Add("action", "get"); webClient.QueryString.Add("id", captcha_id); return webClient.DownloadString("http://2captcha.com/res.php"); } // validate site with returned token thru proxy.php public string getValidate(string token) { WebClient webClient = new WebClient(); webClient.QueryString.Add("response", token); return webClient.DownloadString("http://testing-ground.scraping.pro/proxy.php"); } } class Program { static void Main(string[] args) { if (args.Length != 1) { System.Console.WriteLine("Usage: main.exe <2captcha_service_key>"); return; } string service_key = args[0]; // Console.WriteLine("2captcha service key: " + service_key); Recaptcha2captcha service = new Recaptcha2captcha(); // we set 2captcha service key and target google site_key service.setServiceKey(service_key); service.setSiteKey("6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8"); serivce.service.setPageUrl("http://testing-ground.scraping.pro/recaptcha"); var resp = service.SendRequest(); var gcaptchaToken = ""; Console.WriteLine(resp.Substring( 3, resp.Length-3)); if (resp.Contains("OK|")){ // loop till the service solves captcha and gets g-recaptcha-response token var i=0; while (i++ <= 20) { System.Threading.Thread.Sleep(5000); // sleep 5 seconds Console.WriteLine("Captcha is being solved for {0} seconds", i*5); gcaptchaToken = service.getToken(resp.Substring( 3, resp.Length-3)); if (gcaptchaToken.Contains("OK|")) { break; } } if (gcaptchaToken.Contains("OK|")) { var RecaptchaResponseToken = gcaptchaToken.Substring(3, gcaptchaToken.Length-3); Console.WriteLine("g-recaptcha-response token: " + RecaptchaResponseToken ); // make google to validate g-recaptcha-response token var iSvalid = service.getValidate(RecaptchaResponseToken); Console.WriteLine("Token is validated by google: " + iSvalid ); // submit form to the target site var SubmitFormResp = service.SubmitForm(RecaptchaResponseToken); Console.WriteLine("Submit form return: " + SubmitFormResp ); } else { Console.WriteLine("Captcha has not been solved. Error code: " + gcaptchaToken); //Environment.Exit(0); } } else { Console.WriteLine("Error: " + resp); //Environment.Exit(0); } Console.Read(); } } |
Aleksey
Please fix next typos in example:
1) return “2captcha service return error. Error code:”.responseString; -> + responseString
2) serivce.setPageUrl… -> service.service.setPageUrl
3) uncomment public string getValidate(string token)
Igor Savinkin
Thank you, Aleksey.
Nilesh
How to develop captcha solving software ?