public boolean loginBySavedCookies() { loginCookies.clear(); readCookies(EzraPoundUtil.LOGIN_COOKIES_DIR, loginCookies); Connection con = JsoupUtil.getGetCon("https://www.zhihu.com"); Response rs = null; try { rs = con.cookies(loginCookies).execute(); } catch (IOException e) { e.printStackTrace(); log.info("携带cookie登录测试失败"); return false; } return checkLogin(Jsoup.parse(rs.body())); }
public boolean getXsrf(int times) { if (times > maxRecursiveTimes) return false; Connection con = JsoupUtil.getGetCon("http://www.zhihu.com"); Response rs = null; try { rs = con.execute(); } catch (IOException e) { e.printStackTrace(); log.info("获取_xsrf第" + times + "次失败"); return getXsrf(++times); } Document doc = Jsoup.parse(rs.body()); xsrf = doc.select(".view.view-signin [name=\"_xsrf\"]").attr("value"); log.info("已获得xsrf:" + xsrf); return true; }
public boolean loginByEmailAndPwd() { loginCookies.clear(); Scanner sc = new Scanner(System.in); getCaptchaImgAndCookies(0); log.info("请输入账号:"); email = sc.nextLine(); log.info("请输入密码"); password = sc.nextLine(); log.info("查看验证码并输入"); captcha = sc.nextLine(); Connection con = JsoupUtil.getPostCon("https://www.zhihu.com/login/email"); Response rs = null; try { rs = con.data("_xsrf", xsrf) .data("email", email) .data("password", password) .data("remember_me", remeberMe) .data("captcha", captcha) .cookies(captchaCookies) .ignoreContentType(true) .execute(); } catch (IOException e) { e.printStackTrace(); log.info("通过账号密码登录发生异常"); return false; } JSONObject jsonObject = new JSONObject(rs.body()); String result = jsonObject.get("r").toString(); log.info(EzraPoundUtil.unicode2Character(jsonObject.get("msg").toString())); Response rs2 = null; try { rs2 = JsoupUtil.getGetCon("https://www.zhihu.com").cookies(rs.cookies()).execute(); } catch (IOException e) { e.printStackTrace(); } if (checkLogin(Jsoup.parse(rs2.body()))) { loginCookies.putAll(rs.cookies()); saveCookies(EzraPoundUtil.LOGIN_COOKIES_DIR, loginCookies); return true; } return false; }