模拟登录系列3-java模拟登录网站-验证码破解( 三 )

thread = new ThreadLocal();public static Set codeSet;private static final Logger mylog = Logger.getLogger(CheckCodeUtil.class);;//检查是否public static boolean checkCode(String code) {return codeSet.contains(code);}/*** 干扰码较多或者识别度不高时,调用转换验证码识别接口(这里是公司的识别平台)* @param image_save_path* @param codeType* @param imgurl* @return*/public staticString analysisCode(String image_save_path,String codeType,String imgurl) {//优先调用讯策提供的验证码 解析 。20190426if("3".equals(codeType)){codeType="30400";//4位数字和字母组合}else if("4".equals(codeType)){codeType="10400";//4位数字}else if("5".equals(codeType)){codeType="30500";//5位数字和字母组合}else if("6".equals(codeType)){codeType="30600";//6位数字和字母组合}else if("7".equals(codeType)){codeType="30700";//7位数字和字母组合}else if("8".equals(codeType)){codeType="30800";//8位数字和字母组合}else if("10".equals(codeType)){codeType="50100";//简单计算图}else if("20".equals(codeType)){codeType="50200";//复杂计算图}String code =VerifyCode.getVerifyCode(image_save_path,codeType,imgurl);if(!"".equals(code)){return code;}return code;}public staticString getComplexCode(String image_save_path,String imgurl) {String codeStr="";try {codeStr = MyImgCheck.checkFile(image_save_path);} catch (URISyntaxException e) {mylog.error("调用百度中心接口失败~");e.printStackTrace();} catch (IOException e) {mylog.error("调用百度中心接口失败~");e.printStackTrace();}String code="";try {Map m = JSON.parseObject(codeStr,Map.class);List lst = JSONObject.parseArray(m.get("words_result")+"", String.class);if(lst.size()>0){Map m1= JSON.parseObject(lst.get(0),Map.class);code = m1.get("words")+"";mylog.info("用百度开发者中心接口验证码为:"+code);}}catch (Exception e) {mylog.error("调用百度开发者中心接口返回值 格式解析失败~");mylog.error("错误返回值【"+codeStr+"】");e.printStackTrace();}return code;}/*** * @param in*输入流* @param path*地址* @return 是否成功*/public static synchronized boolean download(InputStream in, String path) {FileOutputStream out = null;try {out = new FileOutputStream(path);// out = new FileOutputStream("D:\\abb.jpg");byte b[] = new byte[1024];int j = 0;while ((j = in.read(b)) != -1) {out.write(b, 0, j);}out.flush();File file = new File(path);if (file.exists() && file.length() == 0)return false;return true;} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {if ("FileNotFoundException".equals(e.getClass().getSimpleName()))System.err.println("download FileNotFoundException");if ("SocketTimeoutException".equals(e.getClass().getSimpleName()))System.err.println("download SocketTimeoutException");elsee.printStackTrace();} finally {if (out != null)try {out.close();} catch (IOException e) {e.printStackTrace();}if (in != null)try {in.close();} catch (IOException e) {e.printStackTrace();}}return false;}public static CloseableHttpClient getConnection() {if (CheckCodeUtil.thread.get() == null) {CookieStore cookieStore = new BasicCookieStore();//httpClient4.x 使用cookie保持会话CheckCodeUtil.thread.set(cookieStore);}CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(CheckCodeUtil.thread.get()).build();return httpClient;}/*** 关闭连接~* */public static void removeConn() {if (CheckCodeUtil.thread.get() != null) {CheckCodeUtil.thread.remove();}}public static String getFileName() {SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");return sdf.format(new Date());}/*** 获取session* * @return*/public static CookieStore getCok() {return CheckCodeUtil.thread.get();}/*** 获取本月最后一天* * @return*/public static String getLastMonth() {Calendar cal_1 = Calendar.getInstance();// 获取当前日期// cal_1.add(Calendar.MONTH, -1);cal_1.set(Calendar.DAY_OF_MONTH, 1);cal_1.add(Calendar.DATE, -1);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.format(cal_1.getTime());}}
状态实体类:.java