Solo  当前访客:2 开始使用

图片验证码识别


1、下载联众平台api
联众识别2.0API接口协议.zip
2、使用demo

/**
 *
 * @param paramMap 验证码格式参数
 * @param uri 联众接口url
 * @param buffer 图片的base64
 * @return
 */
public static JSONObject getVerificationCode(Map<String, String> paramMap,String uri,byte[] buffer){
    JSONObject result = new JSONObject();
    try{
        String BOUNDARY = "---------------------------68163001211748"; //boundary就是request头和上传文件内容的分隔符
        URL url=new URL(uri);
        HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("content-type", "multipart/form-data; boundary="+BOUNDARY);
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);

        // 普通参数
        OutputStream out = new DataOutputStream(connection.getOutputStream());
        if (paramMap != null) {
            StringBuffer strBuf = new StringBuffer();
            Iterator<Map.Entry<String, String>> iter = paramMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String,String> entry = iter.next();
                String inputName = entry.getKey();
                String inputValue = entry.getValue();
                strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
                strBuf.append("Content-Disposition: form-data; name=\""
                        + inputName + "\"\r\n\r\n");
                strBuf.append(inputValue);
            }
            out.write(strBuf.toString().getBytes());
        }

        //图片
        String filename="code.jpg";
        String contentType = "image/jpeg";//这里看情况设置
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
        strBuf.append("Content-Disposition: form-data; name=\""
                + "upload" + "\"; filename=\"" + filename+ "\"\r\n");
        strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
        out.write(strBuf.toString().getBytes());
        out.write(buffer);
        byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
        out.write(endData);
        out.flush();
        out.close();

        //读取URLConnection的响应
        InputStream in = connection.getInputStream();
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        while (true) {
            int rc = in.read(buf);
            if (rc <= 0) {
                break;
            } else {
                bout.write(buf, 0, rc);
            }
        }
        in.close();
        result=JSONObject.fromObject(new String(bout.toByteArray()));
    }catch (Exception e){
        Logger logger = LoggerFactory.getLogger(LianZhong.class);
        logger.error("联众接口获取验证码信息失败:{}",e.getMessage());
    }
    return result;

} 

public Map<String, String> getParam() {
    Map<String, String> paramMap = new HashMap<>();
    paramMap.put("user_name", lzUserName);
    paramMap.put("user_pw", lzPw);
    paramMap.put("yzm_minlen", lzYzmMinle);
    paramMap.put("yzm_maxlen", );
    paramMap.put("yzmtype_mark", lzYzmMark);
    paramMap.put("zztool_token", lzZztoolToken*);
    return paramMap;
}

配置:

ENV.lianzhong.uri=http://v1-http-api.jsdama.com/api.php?mod=php&act=upload
ENV.lianzhong.user_name=ida_9045
ENV.lianzhong.user_pw=ida_904511
ENV.lianzhong.yzm_minle=4
ENV.lianzhong.yzm_maxlen=4
ENV.lianzhong.yzmtype_mark=0
ENV.lianzhong.zztool_token=123

调用示例:

JSONObject result = LianZhong.getVerificationCode(getParam(), lzUri, response.body().bytes());`

二、说明

使用联众打码平台,需要先注册账号

0 0