using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; using System.Threading; using System; namespace MegghysAPI.Modules { public static class CapResolver { private const string ApiKey = "CAP-5537BB057BE85FC11F04BB5BB6E80E73AD44279056AC22D0E70664B24964C3D9"; private const string CreateTaskUrl = "https://api.capsolver.com/createTask"; private const string GetTaskResultUrl = "https://api.capsolver.com/getTaskResult"; private static readonly HttpClient HttpClient = new HttpClient(); private class CreateTaskRequest { [JsonPropertyName("clientKey")] public string ClientKey { get; set; } [JsonPropertyName("task")] public object Task { get; set; } } private class GeetestTask { [JsonPropertyName("type")] public string Type { get; set; } = "GeeTestTaskProxyLess"; [JsonPropertyName("websiteURL")] public string WebsiteURL { get; set; } [JsonPropertyName("gt")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string Gt { get; set; } [JsonPropertyName("challenge")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string Challenge { get; set; } [JsonPropertyName("captchaId")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string CaptchaId { get; set; } [JsonPropertyName("geetestApiServerSubdomain")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string GeetestApiServerSubdomain { get; set; } } private class CreateTaskResponse { [JsonPropertyName("errorId")] public int ErrorId { get; set; } [JsonPropertyName("status")] public string Status { get; set; } [JsonPropertyName("taskId")] public string TaskId { get; set; } } private class GetTaskResultRequest { [JsonPropertyName("clientKey")] public string ClientKey { get; set; } [JsonPropertyName("taskId")] public string TaskId { get; set; } } private class GetTaskResultResponse { [JsonPropertyName("errorId")] public int ErrorId { get; set; } [JsonPropertyName("status")] public string Status { get; set; } [JsonPropertyName("solution")] public GeetestSolution Solution { get; set; } } public class GeetestSolution { [JsonPropertyName("challenge")] public string Challenge { get; set; } [JsonPropertyName("validate")] public string Validate { get; set; } } public static async Task SolveGeetestAsync(string websiteUrl, string gt = null, string challenge = null, string captchaId = null, string apiServerSubdomain = null) { var geetestTask = new GeetestTask { WebsiteURL = websiteUrl, Gt = gt, Challenge = challenge, CaptchaId = captchaId, GeetestApiServerSubdomain = apiServerSubdomain }; var createTaskRequest = new CreateTaskRequest { ClientKey = ApiKey, Task = geetestTask }; var response = await HttpClient.PostAsJsonAsync(CreateTaskUrl, createTaskRequest); if (!response.IsSuccessStatusCode) { // 可以添加日志记录 return null; } var createTaskResponse = await response.Content.ReadFromJsonAsync(); if (createTaskResponse == null || createTaskResponse.ErrorId != 0 || string.IsNullOrEmpty(createTaskResponse.TaskId)) { // 可以添加日志记录 return null; } var getTaskResultRequest = new GetTaskResultRequest { ClientKey = ApiKey, TaskId = createTaskResponse.TaskId }; await Task.Delay(3000); while (true) { await Task.Delay(1000); var resultResponse = await HttpClient.PostAsJsonAsync(GetTaskResultUrl, getTaskResultRequest); if (!resultResponse.IsSuccessStatusCode) { // 可以添加日志记录 continue; } var getTaskResultResponse = await resultResponse.Content.ReadFromJsonAsync(); if (getTaskResultResponse == null) { continue; } if (getTaskResultResponse.Status == "ready") { return getTaskResultResponse.Solution; } if (getTaskResultResponse.Status == "failed" || getTaskResultResponse.ErrorId != 0) { // 可以添加日志记录 return null; } } } } }