private static Response execute( String url, Method method, Map<String, String> cookies, Map<String, String> data) { Response response = null; Connection connection = Jsoup.connect(url); connection.method(method); connection.timeout(10000); connection.ignoreContentType(true); connection.maxBodySize(0); if (cookies != null) { connection.cookies(cookies); } if (data != null) { for (Entry<String, String> entry : data.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); connection.data(key, value); } } try { response = connection.execute(); } catch (IOException e) { e.printStackTrace(); } return response; }
/** * 点赞 * * @param statusId * @param like * @param cookie * @return * @throws TaskException */ public LikeResultBean doLike(String statusId, boolean like, String cookie) throws TaskException { try { String url = like ? "http://m.weibo.cn/attitudesDeal/add" : "http://m.weibo.cn/attitudesDeal/delete"; Map<String, String> cookieMap = new HashMap<String, String>(); String[] cookieValues = cookie.split(";"); for (String cookieValue : cookieValues) { String key = cookieValue.split("=")[0]; String value = cookieValue.split("=")[1]; cookieMap.put(key, value); } // Logger.d(WeiboClientActivity.TAG, cookieMap); Connection connection = Jsoup.connect(url); connection .userAgent( "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:33.0) Gecko/20100101 Firefox/33.0") .referrer("http://m.weibo.cn/") .cookies(cookieMap) .data("id", statusId) .method(Connection.Method.POST); if (like) connection.data("attitude", "heart"); String body = connection.execute().body(); if (!TextUtils.isEmpty(body)) { Logger.d(TAG, body); if (body.indexOf("http://passport.weibo.cn/sso/crossdomain") != -1) throw new TaskException("-100", "未登录"); else if (body.indexOf("<html") != -1) throw new TaskException("-100", "未登录"); LikeResultBean likeBean = JSON.parseObject(body, LikeResultBean.class); if (likeBean.getOk() == 1) { return likeBean; } else if (likeBean.getOk() == -100) { throw new TaskException("-100", "未登录"); } else { throw new TaskException("", likeBean.getMsg()); } } } catch (Exception e) { if (e instanceof TaskException) throw (TaskException) e; e.printStackTrace(); } throw new TaskException(TaskException.TaskError.timeout.toString()); }
@Override protected List<GiveawayGroup> doInBackground(Void... params) { Log.d(TAG, "Fetching giveaways for page " + page); try { // Fetch the Giveaway page Connection jsoup = Jsoup.connect("http://www.steamgifts.com/giveaway/" + path + "/groups/search") .userAgent(Constants.JSOUP_USER_AGENT) .timeout(Constants.JSOUP_TIMEOUT); jsoup.data("page", Integer.toString(page)); if (SteamGiftsUserData.getCurrent(fragment.getContext()).isLoggedIn()) jsoup.cookie( "PHPSESSID", SteamGiftsUserData.getCurrent(fragment.getContext()).getSessionId()); Document document = jsoup.get(); SteamGiftsUserData.extract(fragment.getContext(), document); // Parse all rows of groups Elements groups = document.select(".table__row-inner-wrap"); Log.d(TAG, "Found inner " + groups.size() + " elements"); List<GiveawayGroup> groupList = new ArrayList<>(); for (Element element : groups) { Element link = element.select(".table__column__heading").first(); // Basic information String title = link.text(); String id = link.attr("href").substring(7, 12); String avatar = null; Element avatarNode = element.select(".global__image-inner-wrap").first(); if (avatarNode != null) avatar = Utils.extractAvatar(avatarNode.attr("style")); GiveawayGroup group = new GiveawayGroup(id, title, avatar); groupList.add(group); } return groupList; } catch (IOException e) { Log.e(TAG, "Error fetching URL", e); return null; } }
public static Map<String, String> login( HttpServletRequest req, HttpServletResponse res, String username, String password) throws Exception { String url = ReadProperties.getByName("login.ip") + "/login"; Map<String, String> datas = new HashMap<String, String>(); Map<String, String> cookies = new HashMap<String, String>(); Connection con = Jsoup.connect(url).timeout(120000); // 获取连接 con.header( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"); // 配置模拟浏览器 Response rs; rs = con.execute(); cookies = rs.cookies(); Document doc = Jsoup.parse(rs.body()); // 转换为Dom树 List<Element> et = doc.select("form"); // 获取form表单,可以通过查看页面源码代码得知 for (Element e : et.get(0).getAllElements()) { if (e.attr("name").equals("username")) { e.attr("value", username); // 设置用户名 } if (e.attr("name").equals("password")) { e.attr("value", password); // 设置用户密码 } if (e.attr("name").length() > 0) { // 排除空值表单属性 datas.put(e.attr("name"), e.attr("value")); } } // 设置cookie和post上面的map数据 Response login = null; login = con.data(datas).cookies(cookies).method(Method.POST).execute(); url = ReadProperties.getByName("common.ip") + req.getContextPath() + "/user/getUser"; con = Jsoup.connect(url) .cookies(login.cookies()) .ignoreContentType(true) .method(Method.GET); // 获取连接 rs = con.execute(); for (Entry<String, String> entry : rs.cookies().entrySet()) { Cookie cookie = new Cookie(entry.getKey(), entry.getValue()); cookie.setPath(req.getContextPath() + "/"); res.addCookie(cookie); } return JsonUtil.jsonToObject(rs.body(), Map.class); }
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; }
@Override public void addExtraParameters(Connection connection) { connection.data("code", giveawayId); }