private void login() throws IOException { Response resp = Http.url(this.url).response(); cookies = resp.cookies(); String ctoken = resp.parse().select("form > input[name=ctoken]").first().attr("value"); Map<String,String> postdata = new HashMap<>(); postdata.put("user[login]", new String(Base64.decode("cmlwbWU="))); postdata.put("user[password]", new String(Base64.decode("cmlwcGVy"))); postdata.put("rememberme", "1"); postdata.put("ctoken", ctoken); resp = Http.url("http://en.2dgalleries.com/account/login") .referrer("http://en.2dgalleries.com/") .cookies(cookies) .data(postdata) .method(Method.POST) .response(); cookies = resp.cookies(); }
/** * Login to Flickr. * @return Cookies for logged-in session * @throws IOException */ @SuppressWarnings("unused") private Map<String,String> signinToFlickr() throws IOException { Response resp = Jsoup.connect("http://www.flickr.com/signin/") .userAgent(USER_AGENT) .followRedirects(true) .method(Method.GET) .execute(); Document doc = resp.parse(); Map<String,String> postData = new HashMap<>(); for (Element input : doc.select("input[type=hidden]")) { postData.put(input.attr("name"), input.attr("value")); } postData.put("passwd_raw", ""); postData.put(".save", ""); postData.put("login", new String(Base64.decode("bGVmYWtlZGVmYWtl"))); postData.put("passwd", new String(Base64.decode("MUZha2V5ZmFrZQ=="))); String action = doc.select("form[method=post]").get(0).attr("action"); resp = Jsoup.connect(action) .cookies(resp.cookies()) .data(postData) .method(Method.POST) .execute(); return resp.cookies(); }
private static void postStatsOn(String homeUrl, APIKey token, IShard shard) { try { JSONObject content = new JSONObject() .put("shard_id", shard.getInfo()[0]) .put("shard_count", shard.getInfo()[1]) .put("server_count", shard.getGuilds().size()); Map<String, String> header = new HashMap<>(); header.put("Content-Type", "application/json"); header.put("Authorization", APIKeys.get(token)); String url = String.format("%s/api/bots/%d/stats", homeUrl, shard.getClient().getOurUser().getLongID()); Jsoup.connect(url) .method(Method.POST) .ignoreContentType(true) .headers(header) .requestBody(content.toString()) .post(); } catch (Exception err) { LogUtils.infof("An error occurred while posting statistics of shard %d. (%s: %s).", shard.getInfo()[0], err.getClass().getSimpleName(), err.getMessage()); } }
public static Map<String, String> getNewVersion() throws IOException, JSONException { Connection.Response response = Jsoup .connect(MyStringUtils.CURL + MyStringUtils.API_TAKEN) .method(Method.GET).ignoreContentType(true).timeout(5000) .execute(); JSONObject dataJson = new JSONObject(response.body()); JSONObject dataJson2 = dataJson.getJSONObject("binary"); map.put("name", dataJson.getString("name")); map.put("version", dataJson.getString("version")); map.put("changelog", dataJson.getString("changelog")); map.put("versionShort", dataJson.getString("versionShort")); map.put("direct_install_url", dataJson.getString("direct_install_url")); map.put("fsize", bytes2kb(Long.parseLong(dataJson2.getString("fsize")))); return map; }
/** * Handle authentification if needed for cookie * @return * @throws IOException */ public static Map<String, String> authentificationKi() throws IOException { Connection.Response res = null; if (AuthentificationParam.AUTHENFICATION.isValue()) { Connection conn = getConnection(CommonConst.URL_AUTH_KRALAND, null, null); conn.data("p1", AuthentificationParam.KI_SLAVE_LOGIN.getValue(), "p2", AuthentificationParam.KI_SLAVE_PASS.getValue(), "Submit", "Ok").method(Method.POST).execute(); res = conn.execute(); } else { res = getConnection(CommonConst.URL_KRALAND_MAIN, null, null).execute(); } Map<String, String> cookies = res.cookies(); logger.debug("cookies: {}", cookies); return cookies; }
@Test public void connectDns() throws IOException{ Map m2 = new HashMap(); m2.put("grant_type", "password"); m2.put("username", "test"); m2.put("password", "12345"); Map m = new HashMap(); m.put("resource_type","token_gen_cmd"); m.put("attrs", m2); HttpForm httpForm = HttpForm.gene(). setUrl("https://1.1.1.1/auth_cmd"). setReferrer("https://1.1.1.1"). setMethod(Method.POST). setDataString(JsonUtil.Object2JsonString(m)); String r = httpService.simpleConnect(httpForm ); System.out.println(r); }
/** * 获取群列表 * url : http://qun.qq.com/cgi-bin/qun_mgr/get_group_list * method : post * parameters : bkn * getGroupList 方法 */ @Deprecated public static List<GroupListVO> getGroupList_old(){ try { List<GroupListVO> list = new ArrayList<>(); Map<String, String> data = new HashMap<>(); data.put("bkn", getCsrfToken()); Map<String, String> cookies = getCookies(); String result = WebUtil.fetch("http://qun.qq.com/cgi-bin/qun_mgr/get_group_list", Method.POST, data, cookies); if(result != null){ //System.out.println(result); JSONArray jarr = JSONObject.parseObject(result).getJSONArray("manage"); GroupListVO listVO = null; for(int a=0; a<jarr.size(); a++){ JSONObject obj = jarr.getJSONObject(a); listVO = new GroupListVO(obj.getLong("gc"), obj.getString("gn"), obj.getLong("owner")); list.add(listVO); } return list; } } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
/** * 获取群成员列表方法2 * url : http://qun.qzone.qq.com/cgi-bin/get_group_member?uin=1066231345&groupid=215400054&random=0.15297316415049034&g_tk=277066193 * mehod : get * parameters : * uin = QQ号 * groupid = 群号 * random = 随机数 * g_tk = csrtoken */ @Deprecated public static List<GroupMemberListVO> getGroupMemberList2(String group){ try { List<GroupMemberListVO> voList = new ArrayList<>(); Map<String, String> cookies = getCookies(); String url = "http://qun.qzone.qq.com/cgi-bin/get_group_member?uin="+getLoginQQ() +"&groupid="+group+"&random="+Math.random()+"&g_tk="+getCsrfToken(); System.out.println(url); Map<String, String> data = new HashMap<>(0); String result = WebUtil.fetch(url, Method.GET, data, cookies); if(result != null){ result = result.substring(10, result.length() - 2); JSONArray jarr = JSONObject.parseObject(result).getJSONObject("data").getJSONArray("item"); for(int i=0; i<jarr.size(); i++){ JSONObject obj = jarr.getJSONObject(i); voList.add(new GroupMemberListVO(obj.getString("nick"), obj.getLong("uin"), obj.getInteger("iscreator"), obj.getInteger("ismanager"))); } return voList; } } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
/** * 根据URL和请求类型获取请求结果,用于跨域请求 * @param url * @param urlType POST OR GET * @return */ public static String getUrlResult(String url,String urlType){ Method method = null; if(urlType!= null && !"".equals(urlType) && urlType.equalsIgnoreCase("post")){ method = Method.POST; }else{ method = Method.GET; } Response resp = null; try { resp = Jsoup.connect(url).userAgent("Chrome").timeout(5000).ignoreContentType(true).method(method).execute(); } catch (IOException e) { log.error(WebUtil.class,e); } if(resp == null){ return "error"; }else{ return resp.body(); } }
public BibtexEntry extract(String url) { try { Document doc = Jsoup.connect(url).get(); Element form = doc.select("form[name=exportCite]").first(); Connection con = Jsoup.connect("http://www.sciencedirect.com" + form.attr("action")); for(Element hidden : form.select("input[type=hidden]")) con.data(hidden.attr("name"), hidden.attr("value")); con.data("zone", "exportDropDown"); con.data("citation-type", "BIBTEX"); con.data("format", "cite-abs"); con.data("export", "Export"); con.method(Method.POST); Response res = con.execute(); return BibtexParser.singleFromString(res.body()); } catch (Exception e) { Log.error(e, "Error extracting the url: " + url); } return null; }
public BibtexEntry extract(String url) { try { Connection con = Jsoup.connect("http://ieeexplore.ieee.org/xpl/downloadCitations"); String id = url.substring(url.indexOf("arnumber=")+9); int index = id.indexOf('&'); if(index>0) id = id.substring(0, index); con.data("recordIds", id); con.data("citations-format","citation-abstract"); con.data("download-format", "download-bibtex"); con.method(Method.POST); Response res = con.execute(); return BibtexParser.singleFromString(res.body().replace("<br>", "")); } catch (Exception e) { Log.error(e, "Error extracting the url: " + url); } return null; }
/** * <code> * Login to a website. Find the login URL from the action element of the form that * is used on the login page of that website. * Typically it's best to use the Chrome nework inspector to copy all of the form values * that are submitted. This is especially important when you're talking to a .NET application * as they have crazy viewstate parameters that need to be sent. * </code> * * @param pUrl * The URL to submit the login to. * @param pKeyValues * A splat of key value pairs that will be submitted as part of * the login */ public void login(String pUrl, String... pKeyValues) throws IOException { if (LOG.isTraceEnabled()) { LOG.trace("----------------------"); LOG.trace("Emitter.login() started"); } if (noLoginCache) { LOG.info("Logging in to: " + pUrl); loadRaw(pUrl, Method.POST, pKeyValues); } else { loadOrCache(pUrl, Method.POST, pKeyValues); } if (LOG.isTraceEnabled()) { LOG.trace("Emitter.login() complete"); LOG.trace("----------------------"); } }
private Document loadRaw(String pUrl, Method method, String... pKeyValues) throws IOException { Document newDocument; String cacheDir2 = createCacheDir(); File hashFile2 = new File(cacheDir2 + DigestUtils.md5Hex(extractSaliantParts(pUrl)) + describe(pUrl) + ".html"); ensureCacheDirExists(cacheDir2); Connection connection = createConnection(pUrl); connection.data(pKeyValues); Connection.Response res = execute(method, connection); cookies.putAll(res.cookies()); printCookies("Receiving", res.cookies()); newDocument = res.parse(); final String body = res.body(); if (!containsInvalidCacheContent(body)) { FileUtils.write(hashFile2, body, "UTF8"); } return newDocument; }
private Connection.Response execute(Method method, Connection connection) throws IOException { int requestAttempts = 0; while (true) { try { return connection.method(method).execute(); } catch (IOException e) { if (requestAttempts >= 3) { throw new IOException("Tried connection three times with " + waitTimes[0] + " second, " + waitTimes[1] + " second and " + waitTimes[2] + " second waits.", e); } LOG.warn("Got exception " + requestAttempts + " pausing for " + waitTimes[requestAttempts] + " second(s). Exception was " + e.getMessage()); sleepSeconds(waitTimes[requestAttempts]); requestAttempts++; } } }
protected Document doInBackground(String... urls) { Log.d(LOG_TAG, "doInBackground, url=" + urls[0]); try { Response resp = Jsoup.connect(urls[0]) .header("Authorization", "Basic " + base64login).timeout(30 * 1000) .method(Method.GET).execute(); if (resp.contentType().contains("text/html")) { Log.d(LOG_TAG, "New directory"); currentUrl = urls[0]; return resp.parse(); } else { Log.d(LOG_TAG, "UnsupportedContentType"); return null; } } catch (UnsupportedMimeTypeException me) { Log.d(LOG_TAG, "UnsupportedMimeTypeException"); return null; } catch (Exception e) { Log.d(LOG_TAG, "Other Exception"); this.exception = e; return null; } }
protected Document doInBackground(String... urls) { try { Response resp = Jsoup .connect(urls[0]) .timeout(10 * 1000) .cookie("pianhao", "%7B%22qing%22%3A%22super%22%2C%22qtudou%22%3A%22null%22%2C%22qyouku%22%3A%22null%22%2C%22q56%22%3A%22null%22%2C%22qcntv%22%3A%22null%22%2C%22qletv%22%3A%22null%22%2C%22qqiyi%22%3A%22null%22%2C%22qsohu%22%3A%22null%22%2C%22qqq%22%3A%22null%22%2C%22qku6%22%3A%22null%22%2C%22qyinyuetai%22%3A%22null%22%2C%22qtangdou%22%3A%22null%22%2C%22qxunlei%22%3A%22null%22%2C%22qfunshion%22%3A%22null%22%2C%22qsina%22%3A%22null%22%2C%22qpptv%22%3A%22null%22%2C%22xia%22%3A%22ask%22%2C%22pop%22%3A%22no%22%2C%22open%22%3A%22no%22%7D") .method(Method.GET).execute(); return resp.parse(); } catch (UnsupportedMimeTypeException me) { return null; } catch (Exception e) { this.exception = e; return null; } }
private void defaultSettings() { this.retries = Utils.getConfigInteger("download.retries", 1); connection = Jsoup.connect(this.url); connection.userAgent(AbstractRipper.USER_AGENT); connection.method(Method.GET); connection.timeout(TIMEOUT); connection.maxBodySize(0); }
private Map<String, String> loginer(String name, String password) throws IOException, Exception { trustAllHttpsCertificates(); HostnameVerifier hv = (String urlHostName, SSLSession session) -> { System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost()); return true; }; HttpsURLConnection.setDefaultHostnameVerifier(hv); String url = "https://ice.xjtlu.edu.cn/login/index.php"; Connection con = Jsoup.connect(url).timeout(50000);//获取连接 Response rs = con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586").execute();//获取响应 Document d1 = Jsoup.parse(rs.body()); Map<String, String> info = new HashMap<>(); Elements el = d1.getElementsByClass("form-input"); Elements nameBox = el.select("[name = username]"); nameBox.attr("value", name); Elements passBox = el.select("[name = password]"); passBox.attr("value", password); info.put(nameBox.attr("name"), nameBox.attr("value")); info.put(passBox.attr("name"), passBox.attr("value")); Connection con2 = Jsoup.connect(url).timeout(30000); Response login = con2.ignoreContentType(true).method(Method.POST).data(info).cookies(rs.cookies()).execute(); Map<String, String> map = login.cookies(); if (map.size() > 0) { System.out.println("Successfully login on ice. 已成功登陆ice。"); return map; } else { return null; } }
public Map<String, String> getCourse() { Connection con3 = Jsoup.connect("http://ice.xjtlu.edu.cn/my/index.php?mynumber=-2").timeout(300000); Response visit = null; Document d2 = null; Elements courseTitles = null; try { visit = con3.ignoreContentType(true).method(Method.GET).cookies(session).execute(); } catch (IOException ex) { ex.printStackTrace(); } if (visit != null) { d2 = Jsoup.parse(visit.body()); } if (d2 != null) { // courseTitles = d2.getElementsByClass("title").select("a"); For 15-16 Edition courseTitles = d2.getElementsByClass("coursebox").select("h3"); // For 16-17 Edition } Map<String, String> courseMap = new TreeMap(); for (Element ele : courseTitles) { // courseMap.put(ele.attr("title"), ele.attr("href")); For 15-16 Edition courseMap.put(ele.select("a").attr("title"), ele.select("a").attr("href")); // For 16-17 Edition } this.courseMap = courseMap; return courseMap; }
private void updateContacts() throws IOException { if (updated) { return; } updated = true; String selfResponse = sendRequest(Method.GET, "/users/self/profile").body(); JSONObject selfJSON = new JSONObject(selfResponse); updateUser(selfJSON, false); String profilesResponse = sendRequest(Method.GET, "https://contacts.skype.com/contacts/v2/users/" + getSelfLiveUsername() + "/contacts", true).body(); try { JSONObject json = new JSONObject(profilesResponse); if (json.optString("message", null) != null) { throw new ParseException("Error while parsing contacts response: " + json.optString("message")); } JSONArray profilesJSON = json.getJSONArray("contacts"); for (int i = 0; i < profilesJSON.length(); i++) { User user = updateUser(profilesJSON.getJSONObject(i), true); if (user != null && !user.getUsername().equalsIgnoreCase("echo123")) { skype.addContact(user.getUsername()); } } } catch (JSONException e) { throw new ParseException(e); } }
private Response sendRequest(Method method, String apiPath, boolean absoluteApiPath, String... keyval) throws IOException { String url = absoluteApiPath ? apiPath : SERVER_HOSTNAME + apiPath; Connection conn = Jsoup.connect(url).maxBodySize(100 * 1024 * 1024).timeout(10000).method(method).ignoreContentType(true).ignoreHttpErrors(true); logger.finest("Sending " + method + " request at " + url); if (skypeToken != null) { conn.header("X-Skypetoken", skypeToken); } else { logger.fine("No token sent for the request at: " + url); } conn.data(keyval); return conn.execute(); }
/** * Get an object connection with referer and cookie setted * @param url url to get * @param cookies cookies for the new connection * @param referer * @return a connection */ public static Connection getConnection(String url, Map<String, String> cookies, String referer) { Connection result = null; result = Jsoup.connect(url).method(Method.GET).header("Host", CommonConst.HOST_KRALAND); if (referer != null) { result.referrer(referer); } if (cookies != null) { for (Map.Entry<String, String> entry : cookies.entrySet()) { result.cookie(entry.getKey(), entry.getValue()); } } return result; }
@Test public void testApp() throws IOException{ String loginName = "cuifudong"; String password = "abc123"; String t = System.currentTimeMillis()+""; String source = "0"; Map map = new HashMap(); map.put("loginName",loginName); map.put("password", MD5Util.md5Hex(MD5Util.md5Hex(password))); map.put("source", source); map.put("t",t); String serverTokenClient = loginName+MD5Util.md5Hex(password)+t+source; System.out.println(serverTokenClient); map.put("token", MD5Util.md5Hex(serverTokenClient)); System.out.println(map); StringBuilder sb = new StringBuilder(); sb.append(loginName) .append(DigestUtils.md5Hex(password).toLowerCase()) .append(t) .append(source); System.out.println(sb); String serverToken = DigestUtils.md5Hex(sb.toString()).toLowerCase(); System.out.println(serverToken); HttpForm httpForm = HttpForm.gene(). setUrl("http://opapp.cpusoft.tk/monitor/client/api?login"). setReferrer("http://opapp.cpusoft.tk/"). setMethod(Method.POST). setDataMap(map); String r = httpService.simpleConnect(httpForm ); System.out.println(r); }
/** * 获取好友列表的方法 * url : http://qun.qq.com/cgi-bin/qun_mgr/get_friend_list * method : post * parameters : bkn */ @Deprecated public static List<FriendListVO> getFriendList_old(){ try { List<FriendListVO> voList = new ArrayList<>(); Map<String, String> data = new HashMap<>(); data.put("bkn", getCsrfToken()); Map<String, String> cookies = getCookies(); String result = WebUtil.fetch("http://qun.qq.com/cgi-bin/qun_mgr/get_friend_list", Method.POST, data, cookies); if(result != null){ // System.out.println(result); JSONObject jobj = JSONObject.parseObject(result).getJSONObject("result"); Iterator<String> iter = jobj.keySet().iterator(); while(iter.hasNext()){ String key = iter.next(); JSONObject jsonObject = jobj.getJSONObject(key); jsonObject.getString("gname");//分组名称 JSONArray jarr = jsonObject.getJSONArray("mems");//分组下的成员列表 if(jarr != null && jarr.size() > 0){ for(int i=0; i<jarr.size(); i++){ JSONObject obj = jarr.getJSONObject(i); voList.add(new FriendListVO(obj.getString("name"), obj.getLong("uin"))); } return voList; } } } } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
/** * 根据URL和请求类型获取请求结果,用于跨域请求 * @param url * @param method Method.POST Method.GET * @param data 参数 * @param cookie cookies * @return */ public static String fetch(String url, Method method, Map<String, String> data, Map<String, String> cookie){ Response resp = null; try { resp = Jsoup.connect(url).userAgent("Chrome").timeout(5000) .data(data) .cookies(cookie) .ignoreContentType(true).method(method).execute(); } catch (IOException e) { log.error(WebUtil.class,e); } return resp.body(); }
public AsyncPost(Context context, int positiveMsg, Map<String, String> data, Uri uri, Method method){ this.context = context; this.uri = uri; cookies = LogInActivity.getCookies(context); posMsg = positiveMsg; this.method = method; if (data == null) { this.data = new HashMap<>(); }else{ this.data = data; } }
/** * Construct all inner structure of the form. Its inputs, action and method. * Includes only inputs of known types and inputs with names! * * @param formElement is HTML element extracted from document * @param location is {@link URL} */ public Form(Element formElement, URL location) { //Setting action setAction(location, formElement.attr("action").trim()); //Setting method String method = formElement.attr("method"); this.method = Method.valueOf(method.equals("") ? "GET" : method.trim().toUpperCase()); //Extracting inputs Elements inputs = formElement.getElementsByTag("input"); Elements inputsToSkip = new Elements(); for (Element input : inputs) { if (inputsToSkip.contains(input)) continue; if (Type.containsType(input.attr("type"))) { String name = input.attr("name").trim(); Type type = Type.valueOf(input.attr("type").trim().toUpperCase()); String value = input.attr("value").trim(); switch (type) { case CHECKBOX: this.inputs.add(input.hasAttr("checked") ? new CheckboxInput(name, value, value) : new CheckboxInput(name, value)); break; case RADIO: Elements radioInputs = inputs.select("[type=\"" + Type.RADIO.getType() + "\"][name=\"" + name + "\"]"); this.inputs.add(this.processRadioInputs(name, radioInputs)); inputsToSkip.addAll(radioInputs); break; default: this.inputs.add(new Input(name, type, value)); } } } }
/** * Login to Edline * @param username * @param password * @return response of Edline's connection * @throws IOException by Jsoup */ public static Response login(String username, String password) throws IOException { Response res = Jsoup.connect(EDLINE_URL + "/InterstitialLogin.page").timeout(0).userAgent("Chrome/12.0.742.122").execute(); Map<String, String> preLogCookies = res.cookies(); System.out.println("Pre Login Cookies: " + preLogCookies); res = Jsoup.connect(EDLINE_URL + "/post/InterstitialLogin.page") .data("TCNK", "authenticationEntryComponent", "submitEvent", "1", "guestLoginEvent", "", "enterClicked", "false", "bscf", "", "bscv", "", "targetEntid", "", "ajaxSupported", "yes", "screenName", username, "kclq", password) .cookies(preLogCookies) .userAgent("Chrome/12.0.742.122") .method(Method.POST) .timeout(0) .execute(); loginCookies = res.cookies(); loginCookies.putAll(preLogCookies); System.out.println("Login Cookies: " + loginCookies); System.out.println("Connected to: " + res.url()); if (res.url().toString().contains("Notification.page")) { System.out.println("Username and Password Incorrect"); System.exit(1); } return res; }
public static void notFound(BibtexEntry e) { if (Settings.getInstance().isSendDebug()) { try { Connection con = Jsoup.connect( "http://www.jabref.gummu.de/debug.php").method( Method.POST); con.data("author", "" + e.getField("author")); con.data("title", "" + e.getField("title")); con.data("doi", "" + e.getField("doi")); con.execute(); } catch (Exception ex) { // we don't want to handle this. } } }
private Document loadUrl(String pUrl) throws IOException { Method method = Method.GET; if (LOG.isTraceEnabled()) { LOG.trace("----------------------"); LOG.trace("Emitter.loadUrl() started"); } Document newDocument = loadOrCache(pUrl, method); if (LOG.isTraceEnabled()) { LOG.trace("Emitter.loadUrl() complete"); LOG.trace("----------------------"); } return newDocument; }
private Document loadOrCache(String pUrl, Method method, String... pKeyValues) throws IOException { Document newDocument = null; String cacheDir = createCacheDir(); File hashFile = new File(cacheDir + DigestUtils.md5Hex(extractSaliantParts(pUrl)) + describe(pUrl) + ".html"); if (!noCache && hashFile.exists()) { if (LOG.isInfoEnabled()) { LOG.info("Loading Url: " + pUrl + " from cache"); } ensureCacheDirExists(cacheDir); String documentText = FileUtils.readFileToString(hashFile, "UTF-8"); newDocument = Jsoup.parse(documentText); newDocument.setBaseUri(pUrl); if (LOG.isDebugEnabled()) { LOG.debug("Loaded from cache " + (LOG.isTraceEnabled() ? hashFile.getCanonicalPath() : "")); } if (containsInvalidCacheContent(documentText)) { LOG.debug("Cache invalidated by message"); newDocument = loadRaw(pUrl, method, pKeyValues); } } else { if (LOG.isInfoEnabled()) { LOG.info("Loading Url: " + pUrl); } newDocument = loadRaw(pUrl, method, pKeyValues); } return newDocument; }
private Response getResponse(String url) throws IOException { Logger.debug("getResponse: " + url); Connection connection = Jsoup.connect(url); connection.request().method(Method.GET); connection.ignoreContentType(true); connection.execute(); return connection.response(); }
public Document get() throws IOException { connection.method(Method.GET); return response().parse(); }
public Document post() throws IOException { connection.method(Method.POST); return response().parse(); }
/** * Logs into deviant art. Required to rip full-size NSFW content. * @return Map of cookies containing session data. */ private Map<String, String> loginToDeviantart() throws IOException { // Populate postData fields Map<String,String> postData = new HashMap<>(); String username = Utils.getConfigString("deviantart.username", new String(Base64.decode("Z3JhYnB5"))); String password = Utils.getConfigString("deviantart.password", new String(Base64.decode("ZmFrZXJz"))); if (username == null || password == null) { throw new IOException("could not find username or password in config"); } Response resp = Http.url("http://www.deviantart.com/") .response(); for (Element input : resp.parse().select("form#form-login input[type=hidden]")) { postData.put(input.attr("name"), input.attr("value")); } postData.put("username", username); postData.put("password", password); postData.put("remember_me", "1"); // Send login request resp = Http.url("https://www.deviantart.com/users/login") .userAgent(USER_AGENT) .data(postData) .cookies(resp.cookies()) .method(Method.POST) .response(); // Assert we are logged in if (resp.hasHeader("Location") && resp.header("Location").contains("password")) { // Wrong password throw new IOException("Wrong password"); } if (resp.url().toExternalForm().contains("bad_form")) { throw new IOException("Login form was incorrectly submitted"); } if (resp.cookie("auth_secure") == null || resp.cookie("auth") == null) { throw new IOException("No auth_secure or auth cookies received"); } // We are logged in, save the cookies return resp.cookies(); }
/** * @param userName 用户名 * @param pwd 密码 * @throws Exception */ public static void simulateLogin(String userName, String pwd) throws Exception { /* * 第一次请求 * grab login form page first * 获取登陆提交的表单信息,及修改其提交data数据(login,password) */ // get the response, which we will post to the action URL(rs.cookies()) Connection con = Jsoup.connect(LOGIN_URL); // 获取connection con.header(USER_AGENT, USER_AGENT_VALUE); // 配置模拟浏览器 Response rs = con.execute(); // 获取响应 Document d1 = Jsoup.parse(rs.body()); // 转换为Dom树 List<Element> eleList = d1.select("form"); // 获取提交form表单,可以通过查看页面源码代码得知 // 获取cooking和表单属性 // lets make data map containing all the parameters and its values found in the form Map<String, String> datas = new HashMap<>(); for (Element e : eleList.get(0).getAllElements()) { // 设置用户名 if (e.attr("name").equals("login")) { e.attr("value", userName); } // 设置用户密码 if (e.attr("name").equals("password")) { e.attr("value", pwd); } // 排除空值表单属性 if (e.attr("name").length() > 0) { datas.put(e.attr("name"), e.attr("value")); } } /* * 第二次请求,以post方式提交表单数据以及cookie信息 */ Connection con2 = Jsoup.connect("https://github.com/session"); con2.header(USER_AGENT, USER_AGENT_VALUE); // 设置cookie和post上面的map数据 Response login = con2.ignoreContentType(true).followRedirects(true).method(Method.POST).data(datas).cookies(rs.cookies()).execute(); // 打印,登陆成功后的信息 System.out.println(login.body()); // 登陆成功后的cookie信息,可以保存到本地,以后登陆时,只需一次登陆即可 Map<String, String> map = login.cookies(); for (String s : map.keySet()) { System.out.println(s + " : " + map.get(s)); } }