Skip to content

Commit

Permalink
Added support for setting custom api url. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Mar 28, 2020
1 parent 3c0e300 commit 8ede813
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions 2Captcha.Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ static async Task Foo()
// .. additionally you can pass your own httpClient class
var captchaWithHttpClient = new _2Captcha(" ## YOUR API KEY ## ", new HttpClient());

// Need to set a custom api url? This step is optional.
captcha.SetApiUrl("https://CUSTOM_URL/");

// Get current balance
var balance = await captcha.GetBalance();

Expand Down
21 changes: 16 additions & 5 deletions 2Captcha/_2Captcha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private struct _2CaptchaResultInternal
public string Request;
}

private const string ApiUrl = "https://2captcha.com/";
private string _apiUrl = "https://2captcha.com/";

private readonly HttpClient _httpClient;
private readonly string _apiKey;
Expand All @@ -31,6 +31,17 @@ public _2Captcha(string apiKey, HttpClient httpClient = null)
_apiKey = apiKey;
}

public void SetApiUrl(string url)
{
if (!url.EndsWith("/"))
{
_apiUrl = url + "/";
return;
}

_apiUrl = url;
}

public async Task<_2CaptchaResult> GetBalance()
{
var getData = new List<KeyValuePair<string, string>>
Expand All @@ -40,7 +51,7 @@ public async Task<_2CaptchaResult> GetBalance()
new KeyValuePair<string, string>("json", "1")
};

var inResponse = await _httpClient.PostAsync(ApiUrl + "res.php", new FormUrlEncodedContent(getData));
var inResponse = await _httpClient.PostAsync(_apiUrl + "res.php", new FormUrlEncodedContent(getData));
var inJson = await inResponse.Content.ReadAsStringAsync();

var @in = JsonConvert.DeserializeObject<_2CaptchaResultInternal>(inJson);
Expand All @@ -58,7 +69,7 @@ private async Task<_2CaptchaResult> Solve(string method, int delaySeconds, Multi
httpContent.Add(new StringContent(method), "method");
httpContent.Add(new StringContent("1"), "json");

var inResponse = await _httpClient.PostAsync(ApiUrl + "in.php", httpContent);
var inResponse = await _httpClient.PostAsync(_apiUrl + "in.php", httpContent);
var inJson = await inResponse.Content.ReadAsStringAsync();

var @in = JsonConvert.DeserializeObject<_2CaptchaResultInternal>(inJson);
Expand All @@ -82,7 +93,7 @@ private async Task<_2CaptchaResult> Solve(string method, int delaySeconds, param

postData.AddRange(args);

var inResponse = await _httpClient.PostAsync(ApiUrl + "in.php", new FormUrlEncodedContent(postData));
var inResponse = await _httpClient.PostAsync(_apiUrl + "in.php", new FormUrlEncodedContent(postData));
var inJson = await inResponse.Content.ReadAsStringAsync();

var @in = JsonConvert.DeserializeObject<_2CaptchaResultInternal>(inJson);
Expand All @@ -101,7 +112,7 @@ private async Task<_2CaptchaResult> GetResponse(string solveId)

while (true)
{
var resJson = await _httpClient.GetStringAsync(ApiUrl + $"res.php?key={apiKeySafe}&id={solveId}&action=get&json=1");
var resJson = await _httpClient.GetStringAsync(_apiUrl + $"res.php?key={apiKeySafe}&id={solveId}&action=get&json=1");

var res = JsonConvert.DeserializeObject<_2CaptchaResultInternal>(resJson);
if (res.Status == 0)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var captcha = new _2Captcha(" ## YOUR API KEY ## ");
// .. additionally you can pass your own httpClient class
var captchaWithHttpClient = new _2Captcha(" ## YOUR API KEY ## ", new HttpClient());

// Need to set a custom api url? This step is optional.
captcha.SetApiUrl("https://CUSTOM_URL/");

// Get current balance
var balance = await captcha.GetBalance();

Expand Down

0 comments on commit 8ede813

Please sign in to comment.