-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI2Captcha.cs
More file actions
149 lines (131 loc) · 4.83 KB
/
API2Captcha.cs
File metadata and controls
149 lines (131 loc) · 4.83 KB
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
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace API2Captcha
{
public class TwoCaptchaApi
{
private string key;
private string captchaId;
public TwoCaptchaApi(string key)
{
this.key = key;
}
public float GetBalance()
{
string response = "";
try
{
using (WebClient client = new WebClient())
{
client.QueryString.Add("key", key);
client.QueryString.Add("action", "getbalance");
response = client.DownloadString(settings.url_response);
return float.Parse(response);
}
}
catch (Exception ex)
{
throw new BalanceException(response, ex);
}
}
public string SolveReCaptcha(string googleKey, string pageUrl)
{
captchaId = sendReCaptcha(googleKey, pageUrl);
Thread.Sleep(15 * 1000);
return getResult(captchaId);
}
private string sendReCaptcha(string googleKey, string pageUrl)
{
using (WebClient client = new WebClient())
{
client.QueryString.Add("key", key);
client.QueryString.Add("method", "userrecaptcha");
client.QueryString.Add("googlekey", googleKey);
client.QueryString.Add("pageurl", pageUrl);
string response = client.DownloadString(settings.url_request);
return processResponse(response, "ReCaptcha sending error");
}
}
private string processResponse(string response, string exceptionMessage)
{
string status = response.Split('|')[0];
if (status == "OK")
{
string captchaResponse = response.Remove(0, 3);
return captchaResponse;
}
else if (status == "ERROR")
{
throw new Exception($"{exceptionMessage}: {response}");
}
else if (status == "CAPCHA_NOT_READY")
{
return status;
}
throw new ResponseException($"{exceptionMessage}: {response}");
}
public string SolveCaptcha(string path)
{
captchaId = uploadCaptcha(path);
Thread.Sleep(10 * 1000);
return getResult(captchaId);
}
private string uploadCaptcha(string path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException("Image was not found.");
}
byte[] image = File.ReadAllBytes(path);
using (WebClient client = new WebClient())
{
client.QueryString.Add("key", key);
string response = Encoding.Default.GetString(client.UploadFile(settings.url_request, path));
return processResponse(response, "Captcha uploading error");
}
}
private string getResult(string captchaId)
{
string response = "";
for (int i = 0; i <= 20; i++)
{
using (WebClient client = new WebClient())
{
client.QueryString.Add("key", key);
client.QueryString.Add("action", "get");
client.QueryString.Add("id", captchaId);
response = client.DownloadString(settings.url_response);
string captchaResponse = processResponse(response, "Error while getting captcha response");
if (captchaResponse == "CAPCHA_NOT_READY")
{
Thread.Sleep(5 * 1000);
}
else
{
return captchaResponse;
}
}
}
throw new Exception($"Captcha solve error: {response}");
}
public bool ReportBadCaptcha()
{
using (WebClient client = new WebClient())
{
client.QueryString.Add("key", key);
client.QueryString.Add("action", "reportbad");
client.QueryString.Add("id", captchaId);
string response = client.DownloadString(settings.url_response);
return response.Contains("OK_REPORT_RECORDED");
}
}
}
static class settings
{
public const string url_request = "http://2captcha.com/in.php";
public const string url_response = "http://2captcha.com/res.php";
}
}