Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
  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;
  }
Exemplo n.º 3
0
 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()));
 }
Exemplo n.º 4
0
 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;
 }