Skip to content
CaptchaSonic

SDK C# (.NET)

SDK C#/.NET chính thức cho CaptchaSonic — truyền tải nhị phân gRPC, ưu tiên async, ngoại lệ có kiểu rõ ràng.

SDK C# / .NET chính thức cho API giải CAPTCHA CaptchaSonic.

Truyền tải

Sử dụng gRPC gốc qua Grpc.Net.Client. Ảnh được gửi dưới dạng nhị phân thô, không có chi phí base64.

Cài đặt

dotnet add package CaptchaSonic

Bắt đầu nhanh

using CaptchaSonic;

await using var client = new CaptchaSonicClient(new CaptchaSonicOptions
{
    ApiKey = "sonic_your_api_key"
});

double balance = await client.GetBalanceAsync();
Console.WriteLine($"Balance: ${balance:F4}");

byte[] tile = await File.ReadAllBytesAsync("tile.png");
var result = await client.SolvePopularCaptchaAsync(
    images: new[] { tile },
    question: "Select all traffic lights",
    questionType: "objectClassify"
);
Console.WriteLine(string.Join(", ", result.TypedSolution.Grid.Objects));

Hàm khởi tạo đơn giản

await using var client = new CaptchaSonicClient("sonic_your_api_key");

Cấu hình

var client = new CaptchaSonicClient(new CaptchaSonicOptions
{
    ApiKey      = "sonic_your_api_key",          // required, must start with "sonic_"
    Endpoint    = "https://api.captchasonic.com", // optional, default shown
    CallTimeout = TimeSpan.FromSeconds(30),       // optional, per-call gRPC deadline
});

Client này an toàn với đa luồng — hãy tạo một instance và tái sử dụng trong toàn bộ ứng dụng.


Bộ giải ảnh

SolvePopularCaptchaAsync()

await client.SolvePopularCaptchaAsync(
    images: new[] { tile },
    question: "Select all traffic lights",
    questionType: "objectClassify",   // "objectClassify" | "objectClick" | "objectDrag" | "grid"
    examples: null,                   // optional reference images
    screenshot: false,                // optional
    websiteUrl: "https://example.com", // optional, billing context
    websiteKey: null                  // optional, billing context
);

SolveRecaptchaV2Async()

await client.SolveRecaptchaV2Async(
    images: new[] { tile },
    question: "traffic lights",       // or "/m/015qff"
    questionType: "split_33",         // "split_33" | "33" | "44". Optional.
    websiteUrl: null,                 // optional
    websiteKey: null                  // optional
);

SolveGeetestAsync()

await client.SolveGeetestAsync(
    geetestType: "nine",              // "nine" | "click" | "slide" | "match" | "winlinze"
    question: "the bear",             // required for "nine" and "click"
    images: new[] { tile },           // required for "nine", "click", "slide"
    examples: null,                   // optional reference images
    image: null,                      // slice piece for "slide"
    gtv: 3,                           // Geetest version, default 3
    websiteUrl: null                  // optional
);

SolveOcrAsync()

await client.SolveOcrAsync(
    images: new[] { tile },
    module: "common",                 // "common" | "mtcaptcha" | "bls" | "morocco"
    websiteUrl: null                  // optional
);
// Returns: result.TypedSolution.Text.Texts[0]

SolveTikTokAsync()

await client.SolveTikTokAsync(
    type: "whirl",                    // "click" | "whirl" | "slide"
    question: "",                     // optional
    images: new[] { tile },
    examples: null,                   // required for "whirl" and "slide"
    websiteUrl: null                  // optional
);

SolveBinanceAsync()

await client.SolveBinanceAsync(
    type: "grid",                     // "grid" | "slide"
    question: "Select the bicycle",   // required for "grid"
    images: new[] { tile },
    examples: null,                   // optional
    websiteUrl: null                  // optional
);

SolveAwsWafAsync()

await client.SolveAwsWafAsync(
    images: new[] { tile },
    question: "grid:vehicles:cars",
    websiteUrl: null,                 // optional
    websiteKey: null                  // optional
);

SolveSlideImageAsync()

await client.SolveSlideImageAsync(images: new[] { background, piece });
// Returns: result.TypedSolution.Slide.X — pixel offset

Các phương thức tự động hóa token

Các phương thức này gửi tác vụ và thăm dò cho đến khi nhận được token (tối đa 120 giây).

await client.SolveTurnstileAsync(websiteUrl, websiteKey, proxy?);
await client.SolvePopularCaptchaTokenAsync(websiteUrl, websiteKey, proxy?);
await client.SolveRecaptchaV2TokenAsync(websiteUrl, websiteKey, proxy?);
await client.SolveRecaptchaV3TokenAsync(websiteUrl, websiteKey, proxy?);
await client.SolveCloudflareAsync(websiteUrl, websiteKey, proxy);  // proxy required

Các phương thức tiện ích

double balance = await client.GetBalanceAsync();
var health     = await client.HealthCheckAsync();
var result     = await client.GetTaskResultAsync(taskId);
var response   = await client.CreateTaskAsync(task);

Xử lý lỗi

try
{
    var result = await client.SolvePopularCaptchaAsync(images, question, questionType);
}
catch (InvalidApiKeyException)         { /* errorId=1 */ }
catch (InsufficientBalanceException)   { /* errorId=2 */ }
catch (DailyLimitExceededException)    { /* errorId=3 */ }
catch (MinuteLimitExceededException)   { /* errorId=4 */ }
catch (QuotaExceededException)         { /* errorId=5 */ }
catch (PlanExpiredException)           { /* errorId=6 */ }
catch (CaptchaSonicException)          { /* other errors */ }

SDK tự động thử lại khi gặp Unavailable, ResourceExhausted, DeadlineExceeded, với thời gian chờ tăng dần theo cấp số nhân.