Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.ClientProtocolException;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.impl.client.CloseableHttpClient;
  7. import org.apache.http.impl.client.HttpClientBuilder;
  8. import org.json.simple.JSONArray;
  9. import org.json.simple.JSONObject;
  10. import org.json.simple.JSONValue;
  11. import org.json.simple.parser.ParseException;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.util.HashMap;
  17. import java.util.Random;
  18.  
  19. public class ApacheHttpClientGetPost {
  20.     private static final String CATEGORY = "тестовая-страница";
  21.     private static final String GET_URL = "https://public-api.wordpress.com/rest/v1.1/sites/loadtestweb.wordpress.com/posts/?category=";
  22.  
  23.     public static String getJSON() {
  24.         String jsonString = "";
  25.         try {
  26.  
  27.             HttpClient httpClient = HttpClientBuilder.create().build();
  28.             HttpGet getRequest = new HttpGet(GET_URL + CATEGORY);
  29.             getRequest.addHeader("accept", "application/json");
  30.  
  31.             HttpResponse response = httpClient.execute(getRequest);
  32.  
  33.             if (response.getStatusLine().getStatusCode() != 200) {
  34.                 throw new RuntimeException("Failed : HTTP error code : "
  35.                         + response.getStatusLine().getStatusCode());
  36.             }
  37.  
  38.             BufferedReader br = new BufferedReader(
  39.                     new InputStreamReader((response.getEntity().getContent())));
  40.  
  41.             StringBuffer result = new StringBuffer();
  42.             String line = "";
  43.             while ((line = br.readLine()) != null) {
  44.                 result.append(line);
  45.             }
  46.             jsonString = result.toString();
  47.             br.close();
  48.             ((CloseableHttpClient) httpClient).close();
  49.         } catch (ClientProtocolException e) {
  50.             e.printStackTrace();
  51.         } catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.         return jsonString;
  55.     }
  56.  
  57.     public static String getRandomArticleUrl(String jsonString) {
  58.         String randomURL = "";
  59.         try {
  60.             JSONObject articles = (JSONObject) JSONValue.parseWithException(jsonString);
  61.             JSONArray jsonArray = (JSONArray) articles.get("posts");
  62.             Random random = new Random();
  63.             int n = random.nextInt(jsonArray.size());
  64.             HashMap targetURL = (HashMap) jsonArray.get(n);
  65.             randomURL = (String) targetURL.get("URL");
  66.         } catch (ParseException e) {
  67.             e.printStackTrace();
  68.         }
  69.         return randomURL;
  70.     }
  71.  
  72.     public static void openUrl(String url) {
  73.         HttpClient httpClient = HttpClientBuilder.create().build();
  74.  
  75.         HttpPost postRequest = new HttpPost(url);
  76.         postRequest.addHeader("accept", "application/json");
  77.         try {
  78.             HttpResponse response = httpClient.execute(postRequest);
  79.             if (response.getStatusLine().getStatusCode() != 200) {
  80.                 throw new RuntimeException("Failed : HTTP error code : "
  81.                         + response.getStatusLine().getStatusCode());
  82.             }
  83.         } catch (IOException e) {
  84.             e.printStackTrace();
  85.         }
  86.     }
  87.  
  88.     public static void main(String[] args) {
  89.         String jsonResult = getJSON();
  90.         String randomURL = getRandomArticleUrl(jsonResult);
  91.         openUrl(randomURL);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement