// FastOcr.cs — fastOcr 验证码识别 C# SDK(零依赖,单文件) // 用法: 把此文件复制到项目中,var client = new FastOcr("your_api_key"); using System; using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; public class FastOcr { private readonly string _apiKey; private readonly string _baseUrl; private static readonly HttpClient _http = new HttpClient(); public FastOcr(string apiKey, string baseUrl = "http://shenzheng.cstext.top:8989") { _apiKey = apiKey; _baseUrl = baseUrl.TrimEnd('/'); } public async Task RecognizeAsync(string type, string imagePath, string piecePath = null) { var imgB64 = Convert.ToBase64String(File.ReadAllBytes(imagePath)); var body = $"{{\"type\":\"{type}\",\"image\":\"{imgB64}\""; if (piecePath != null) { var pieceB64 = Convert.ToBase64String(File.ReadAllBytes(piecePath)); body += $",\"piece\":\"{pieceB64}\""; } body += "}"; return await PostAsync("/api/v1/captcha/recognize", body); } public async Task GetBalanceAsync() { return await PostAsync("/api/v1/user/balance", "{}"); } private async Task PostAsync(string path, string body) { var req = new HttpRequestMessage(HttpMethod.Post, _baseUrl + path); req.Headers.Add("Authorization", $"Bearer {_apiKey}"); req.Content = new StringContent(body, Encoding.UTF8, "application/json"); var resp = await _http.SendAsync(req); return await resp.Content.ReadAsStringAsync(); } }