/** * 下载验证码图片并保存到手机内部空间<br /> * 下载完后会发送一条广播给《一键签到》主程序,让其显示验证码输入框 * @param captchaUrl * @param ua * @param cookies * @param siteName * @param user * @param reason * @return */ private static boolean downloadCaptchaPic(String captchaUrl, String ua, HashMap<String, String> cookies, String siteName, String user, String reason) { Response res; boolean isSucceed = false; for(int i=0;i<RETRY_TIMES;i++) { try { res = Jsoup.connect(captchaUrl).cookies(cookies).userAgent(ua).timeout(TIME_OUT).ignoreContentType(true).referrer(captchaUrl).method(Method.GET).execute(); cookies.putAll(res.cookies()); try { deleteCaptchaFile();//删除遗留的验证码 saveCaptchaToFile(res.bodyAsBytes());//保存验证码到文件 sendShowCaptchaDialogBC(siteName, user, reason);//给《一键签到》主程序发送广播,让其显示验证码 isSucceed = true; pauseThread();//用线程锁暂停签到线程,如果按下了验证码输入窗口的“确定”或“取消”,程序会对签到线程进行解锁 break;//跳出重试 } catch (Exception e) { //保存验证码到文件失败 isSucceed = false; e.printStackTrace(); } } catch (IOException e) { //拉取验证码失败 isSucceed = false; e.printStackTrace(); } } return isSucceed; }
public boolean getCaptchaImgAndCookies(int times) { captchaCookies.clear(); if (times > maxRecursiveTimes) return false; Connection con = JsoupUtil.getResourceCon( "https://www.zhihu.com/captcha.gif?r=" + System.currentTimeMillis() + "&type=login"); Response rs = null; try { rs = con.execute(); } catch (IOException e) { e.printStackTrace(); log.info("获取验证码第" + times + "次失败"); return getCaptchaImgAndCookies(++times); } File file = new File(EzraPoundUtil.CAPTCHA_DIR); try { FileOutputStream out = (new FileOutputStream(file)); out.write(rs.bodyAsBytes()); } catch (IOException e) { e.printStackTrace(); } captchaCookies.putAll(rs.cookies()); log.info("验证码已保存" + ",路径为:" + file.getAbsolutePath()); log.info("验证码对应cookie为:" + captchaCookies); return true; }