using System; using System.IO; using System.Net; using System.Text; /// /// ============================================================= /// 易盾识别 Demo (C#版) /// /// 功能说明: /// 提交易盾验证码识别任务 /// /// 使用方法: /// 1. 将下方的 APPKEY 替换为你自己的用户密钥 /// 2. 将 YIDUN_ID 替换为你获取到的易盾验证码 id 参数 /// 3. 将 YIDUN_REFERER 替换为你获取到的 referer 值 /// 4. 编译运行:csc RecognizeYidun.cs && RecognizeYidun.exe /// 或使用 dotnet run /// ============================================================= /// class RecognizeYidun { // ======================== 配置区域(请替换为你自己的参数)======================== // 用户密钥,登录平台后获取 static string APPKEY = "你的appkey"; // 易盾验证码的 id 参数值(从目标网站抓包获取) static string YIDUN_ID = "你获取到的易盾id"; // 易盾验证码的 referer 值(从目标网站抓包获取,注意不是当前页面的URL) static string YIDUN_REFERER = "你获取到的referer地址"; // 项目类型,易盾固定填写 500(特殊类型请联系客服) static int ITEM_ID = 500; // 可选参数:代理IP(格式示例:http://IP:端口,若需白名单支持请联系客服。http://账号:密码@proxy.com:8080 或 socks5://127.0.0.1:9888) static string PROXY = ""; // 可选参数:开发者密钥(如果有的话填写) static string DEVKEY = ""; // =============================================================================== // API基础地址 static string API_BASE = "http://api.ttocr.com/api"; /// /// 发送POST请求(表单数据) /// static string HttpPost(string url, string postData) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 120000; // 识别可能需要较长时间 byte[] data = Encoding.UTF8.GetBytes(postData); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Console.WriteLine(new string('*', 60)); Console.WriteLine(" 易盾识别 Demo (C#版)"); Console.WriteLine(new string('*', 60)); // 检查参数是否已配置 if (APPKEY == "你的appkey") { Console.WriteLine("\n[错误] 请先在代码顶部的配置区域填写你的 APPKEY"); Environment.Exit(1); } if (YIDUN_ID == "你获取到的易盾id") { Console.WriteLine("\n[错误] 请先在代码顶部的配置区域填写 YIDUN_ID 和 YIDUN_REFERER"); Environment.Exit(1); } Console.WriteLine(new string('=', 50)); Console.WriteLine("[易盾识别] 正在提交易盾识别任务..."); Console.WriteLine(new string('=', 50)); try { string url = API_BASE + "/recognize2"; // 构建POST请求数据 StringBuilder postData = new StringBuilder(); postData.Append("appkey=").Append(Uri.EscapeDataString(APPKEY)); postData.Append("&id=").Append(Uri.EscapeDataString(YIDUN_ID)); postData.Append("&referer=").Append(Uri.EscapeDataString(YIDUN_REFERER)); postData.Append("&itemid=").Append(ITEM_ID); // 如果设置了代理,则添加代理参数 if (!string.IsNullOrEmpty(PROXY)) { postData.Append("&proxy=").Append(Uri.EscapeDataString(PROXY)); } // 如果设置了开发者密钥,则添加 if (!string.IsNullOrEmpty(DEVKEY)) { postData.Append("&devkey=").Append(Uri.EscapeDataString(DEVKEY)); } Console.WriteLine("[请求参数] id=" + YIDUN_ID); Console.WriteLine("[请求参数] referer=" + YIDUN_REFERER); Console.WriteLine("[请求参数] itemid=" + ITEM_ID); string result = HttpPost(url, postData.ToString()); Console.WriteLine("[完整响应] " + result); } catch (Exception e) { Console.WriteLine("[异常] 网络请求出错:" + e.Message); } Console.WriteLine(); Console.WriteLine(new string('=', 50)); Console.WriteLine("[完成] 识别执行完毕"); Console.WriteLine(new string('=', 50)); } }