Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.JSONValue;
- import org.json.simple.parser.ParseException;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Random;
- public class ApacheHttpClientGetPost {
- private static final String CATEGORY = "тестовая-страница";
- private static final String GET_URL = "https://public-api.wordpress.com/rest/v1.1/sites/loadtestweb.wordpress.com/posts/?category=";
- public static String getJSON() {
- String jsonString = "";
- try {
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpGet getRequest = new HttpGet(GET_URL + CATEGORY);
- getRequest.addHeader("accept", "application/json");
- HttpResponse response = httpClient.execute(getRequest);
- if (response.getStatusLine().getStatusCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatusLine().getStatusCode());
- }
- BufferedReader br = new BufferedReader(
- new InputStreamReader((response.getEntity().getContent())));
- StringBuffer result = new StringBuffer();
- String line = "";
- while ((line = br.readLine()) != null) {
- result.append(line);
- }
- jsonString = result.toString();
- br.close();
- ((CloseableHttpClient) httpClient).close();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return jsonString;
- }
- public static String getRandomArticleUrl(String jsonString) {
- String randomURL = "";
- try {
- JSONObject articles = (JSONObject) JSONValue.parseWithException(jsonString);
- JSONArray jsonArray = (JSONArray) articles.get("posts");
- Random random = new Random();
- int n = random.nextInt(jsonArray.size());
- HashMap targetURL = (HashMap) jsonArray.get(n);
- randomURL = (String) targetURL.get("URL");
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return randomURL;
- }
- public static void openUrl(String url) {
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpPost postRequest = new HttpPost(url);
- postRequest.addHeader("accept", "application/json");
- try {
- HttpResponse response = httpClient.execute(postRequest);
- if (response.getStatusLine().getStatusCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatusLine().getStatusCode());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- String jsonResult = getJSON();
- String randomURL = getRandomArticleUrl(jsonResult);
- openUrl(randomURL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement