調用打碼平台api獲取驗證碼 (C#版)


 一、打碼平台很多,這里選擇兩個:聯眾和斐斐

聯眾開發文檔:

https://www.jsdati.com/docs/guide

斐斐開發文檔:

http://docs.fateadm.com/web/#/1?page_id=1

二、聯眾打碼

從文檔可以看到聯眾的接口是 https://v2-api.jsdama.com/upload

大多數驗證碼類型是四位數字字母,從這里可以看到我們需要的類型是1001.查看類型地址:https://www.jsdati.com/docs/price

獲取需要的參數並發送post請求即可:

    public class LianzhongRun
    {
        private const string ApiCode = "https://v2-api.jsdama.com/upload";
        public static string LianZhongCode(string imgUrl)
        {
            var img64 = NetHandle.GetImageAsBase64Url(imgUrl).Result;
            LianZhongRequestModel param = new LianZhongRequestModel();
            param.captchaData = img64;
            param.softwareId = 0;
            param.softwareSecret = "";
            param.username = "";
            param.password = "";
            // captchaType 類型,查看:https://www.jsdati.com/docs/price
            param.captchaType = 1001;
            using (var _client = new HttpClient())
            {
                _client.DefaultRequestHeaders.Add("host", "v2-api.jsdama.com");
                _client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36");
                StringContent content = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8,
                                "application/json");
                HttpResponseMessage response = _client.PostAsync(ApiCode, content).Result;
                string result = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine("返回結果--" + result);
                if (!result.Contains("recognition")) return string.Empty;
                dynamic resultObj = JsonConvert.DeserializeObject(result);
                var data = resultObj["data"];
                string recognition = data["recognition"];
                Console.WriteLine("驗證碼:" + recognition);
                return recognition.Trim();
            }
        }
    }

三、斐斐打碼

同樣的方式,斐斐接口的請求代碼如下

    public class FeifeiRun
    {
        private static string ApiCode = "http://pred.fateadm.com/api/capreg";
        private static string PdId = "";
        private static string PdKey = "";
        private static string AppKey = "";
        private static string AppId = "";

        /// <summary>
        /// 斐斐打碼
        /// </summary>
        /// <param name="imgUrl"></param>
        /// <returns></returns>
        public static string FeifeiCode(string imgUrl)
        {
            var img64 = NetHandle.GetImageAsBase64Url(imgUrl).Result;
            var timestamp = TimeHelper.GetCurrentTimeUnix();
            string cur_tm = TimeHelper.GetCurrentTimeUnix();
            string sign = SecurityHelper.CalcSign(PdId, PdKey, cur_tm);
            string asign = SecurityHelper.CalcSign(AppId, AppKey, cur_tm);
            var predict_type = "30400";
            var imgBytes = NetHandle.ReadBytes(imgUrl);
            var values = new Dictionary<string, string>
            {
                { "user_id",PdId},
                { "timestamp",timestamp},
                { "sign",sign},
                { "app_id",AppId},
                { "asign",asign},
                { "predict_type",predict_type},
                { "img_data",img64}
            };

            using (var _client = new HttpClient())
            {
                var content = new FormUrlEncodedContent(values);
                HttpResponseMessage response = _client.PostAsync(ApiCode, content).Result;
                string result = response.Content.ReadAsStringAsync().Result;
                var data = JsonConvert.DeserializeObject<HttpRspData>(result);
                if (!string.IsNullOrEmpty(data.RspData))
                {
                    // 附帶附加信息
                    HttpExtraInfo einfo = JsonConvert.DeserializeObject<HttpExtraInfo>(data.RspData);
                    data.einfo = einfo;
                }
                var resultCode = data.einfo.result.Trim();
                Console.WriteLine("code:" + resultCode);
                return resultCode;
            }
        }
    }

 

五、完整代碼

https://github.com/DavideYang125/VerificationCodeObtainDemo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM