private JSONArray getJsonArrayFromURL(URL url) throws IOException { // Wait 2 seconds before the next request long timeDiff = System.currentTimeMillis() - lastRequestTime; if (timeDiff < SLEEP_TIME) { try { Thread.sleep(timeDiff); } catch (InterruptedException e) { LOGGER.warn("[!] Interrupted while waiting to load next page", e); return new JSONArray(); } } lastRequestTime = System.currentTimeMillis(); String jsonString = Http.url(url) .ignoreContentType() .userAgent(REDDIT_USER_AGENT) .response() .body(); Object jsonObj = new JSONTokener(jsonString).nextValue(); JSONArray jsonArray = new JSONArray(); if (jsonObj instanceof JSONObject) { jsonArray.put(jsonObj); } else if (jsonObj instanceof JSONArray) { jsonArray = (JSONArray) jsonObj; } else { LOGGER.warn("[!] Unable to parse JSON: " + jsonString); } return jsonArray; }
public JSONObject getJSON() throws IOException { ignoreContentType(); String jsonString = response().body(); return new JSONObject(jsonString); }
.ignoreContentType() .connection() .execute().body(); jsonString = jsonString.replace(""", "\""); return new JSONObject(jsonString);
List<LagouJobInfo> lagouJobInfos = ParseUtils.result2LagouJobInfoList(res.body());
String pageSource = resp.body(); return pageSource; } catch (IOException e) {
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 String request(String api, Map<String, Object> param) { String url = String.format("%s/client/api/%s", rbacShiroUrl, api); try { Connection.Response response = Jsoup.connect(url) .method(Connection.Method.POST) .timeout(300) .data(ImmutableMap.of( "code", URLEncoder.encode(RSAUtil.encrypt(param, publicKey), "UTF-8"), "uuid", uuid, "licenseKey", licenseKey )).ignoreContentType(true) .execute(); JSONObject object = JSONObject.parseObject(response.body(), JSONObject.class); if (!object.getBoolean("success")) { throw new AuthenticationException(response.body()); } String data = object.getString("data"); JSONObject client = JSON.parseObject(data); String encryptKey = client.getString("key"); byte[] jsonBytes = RSAUtil.decrypt(Base64.getDecoder().decode(encryptKey), publicKey); String key = new String(jsonBytes, "UTF-8"); return DESUtil.decrypt(client.getString("code"), key); } catch (Exception e) { throw new AuthenticationException("请求异常", e); } }
private String getToken(String token, String scope) throws IOException { Response response = Jsoup.connect(SERVER_HOSTNAME + "/oauth20_token.srf").data("client_id", "00000000480BC46C", "scope", scope, "grant_type", "refresh_token", "refresh_token", token).maxBodySize(100 * 1024 * 1024).timeout(10000).method(Method.POST).ignoreContentType(true).ignoreHttpErrors(true).execute(); if (response.statusCode() != 200) { try { JSONObject json = new JSONObject(response.body()); String errorDescription = json.getString("error_description"); IOException ex = new IOException("Error while connecting to Live: token request error: " + errorDescription); logger.log(Level.SEVERE, "", ex); throw ex; } catch (JSONException | IllegalFormatException e) { IOException ex = new IOException("Error while connecting to Live: unknown token request error: " + response.body()); logger.log(Level.SEVERE, "", ex); throw ex; } } try { JSONObject json = new JSONObject(response.body()); String accessToken = json.getString("access_token"); return accessToken; } catch (JSONException | IllegalFormatException e) { IOException ex = new IOException("Error while connecting to Live: failed reading token response: " + response.body()); logger.log(Level.SEVERE, "", ex); throw ex; } }
@SuppressWarnings("unchecked") private List<PluginHolder> fetchPluginsFromGithub() { Object plugins = avaire.getCache().getAdapter(CacheType.MEMORY).remember("plugins", 10800, () -> { try { Connection.Response execute = Jsoup.connect("https://raw.githubusercontent.com/avaire/plugins/master/plugins.json") .ignoreContentType(true).execute(); JSONObject obj = new JSONObject(execute.body()); JSONArray data = obj.getJSONArray("data"); List<PluginHolder> pluginList = new ArrayList<>(); for (Object aData : data) { pluginList.add(new PluginHolder((JSONObject) aData)); } return pluginList; } catch (IOException e) { AvaIre.getLogger().error("Failed to fetch plugins from github: " + e.getMessage(), e); return null; } }); if (plugins instanceof List) { return (List<PluginHolder>) plugins; } return null; } }
/** * Method which retrieve tags from the WS (old shaarli) * Assume being logged in */ public String[] retrieveTagsFromWs() { final String requestUrl = this.mShaarliUrl + "?ws=tags&term=+"; String[] predictionsArr = {}; try { String json = this.createShaarliConnection(requestUrl, true) .ignoreContentType(true) .execute() .body(); JSONArray ja = new JSONArray(json); predictionsArr = new String[ja.length()]; for (int i = 0; i < ja.length(); i++) { // add each entry to our array predictionsArr[i] = ja.getString(i); } } catch (Exception e) { this.mLastError = e; return predictionsArr; } return predictionsArr; }
private String getRandomSong(final String category) { Object cacheItem = avaire.getCache().getAdapter(CacheType.FILE).remember("music-type." + category.toLowerCase(), 31536000, () -> { try { String item = Jsoup.connect( String.format("https://libraries.amped.fm/libraries/%s/musicbot", category) ).execute().body(); String[] items = item.split("\n"); return Arrays.asList(Arrays.copyOfRange(items, 2, items.length)); } catch (IOException e) { AvaIre.getLogger().error("Failed to load {} music category from libraries.amped.fm", category.toLowerCase(), e); return null; } }); if (!(cacheItem instanceof ArrayList)) { return null; } return (String) RandomUtil.pickRandom((List<String>) cacheItem); }
@Override public String execute() { Map<String, Object> resMap = new HashMap<>(); try { Connection.Response response = connection.execute(); resMap.put("code", response.statusCode()); resMap.put("message", response.statusMessage()); resMap.put("body", response.body()); } catch (IOException e) { e.printStackTrace(); resMap.put("code", "-1"); resMap.put("message", "Request Exception"); resMap.put("body", ""); } return gson.toJson(resMap); }
/** * {@inheritDoc} */ @Override public Integer process(Connection.Response response) { try { return Optional.ofNullable(new JsonParser().parse(response.body())) .map(e -> e.isJsonObject() ? e.getAsJsonObject() : null) .map(e -> e.get("status")) .map(JsonElement::getAsInt) .orElse(0); } catch (JsonParseException ignore) { return 0; } } }
private SemanticVersion getLatestVersion() { Object version = avaire.getCache().getAdapter(CacheType.FILE).remember("github.version", 1800, () -> { try { return Jsoup.connect("https://raw.githubusercontent.com/avaire/avaire/master/build.gradle") .execute().body().split("version = '")[1].split("'")[0]; } catch (IOException e) { AvaIre.getLogger().error("Failed to get latest version from github", e); return null; } }); if (version == null) { return null; } return new SemanticVersion(version.toString()); }
public String getPageBody() { return getResponse().body(); }
private int getLatestComicNumber() { return ((Double) avaire.getCache().getAdapter(CacheType.FILE).remember("xkcd.latest", 720, () -> { try { Connection.Response response = Jsoup.connect("https://xkcd.com/info.0.json") .ignoreContentType(true).execute(); return new JSONObject(response.body()).get("num"); } catch (IOException e) { e.printStackTrace(); return 1; } })).intValue(); } }
public JSONObject getJsonResponse() { String clean = getResponse().body().replace("&", "&").replace(""", "\""); return new JSONObject(clean); }
/** * @return Authorization string or null if response is empty */ private ResponseProcessor<String> getResponseProcessor() { return response -> Optional.ofNullable(Jsoup.parse(response.body())) .map(e -> e.select("[data-auth]").first()) .map(e -> e.attr("data-auth")) .orElse(null); } }
public void updateUser(User user) throws IOException { String reponse = sendRequest(Method.GET, "/users/" + user.getUsername() + "/profile/public", "clientVersion", "0/7.12.0.101/").body(); JSONObject userJSON = new JSONObject(reponse); userJSON.put("username", user.getUsername()); updateUser(userJSON, false); }
/** * {@inheritDoc} */ @Override public List<Movie> parseResponse(@NonNull Connection.Response response) { return parser.parse(response.body()); } }