Advertisement
Guest User

RestApiGet

a guest
Feb 12th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package com.company;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.Base64;
  9.  
  10. public class RestApiGet {
  11.     private StringBuffer response = new StringBuffer();
  12.     private   String output;
  13.  
  14.     String getOutput()
  15.     {
  16.         return this.output;
  17.     }
  18.  
  19.     void  setOutput(String username, String password, String apiCall) {
  20.  
  21.         String login = username + ":" + password;
  22.         final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
  23.         String encoded = Base64.getEncoder().withoutPadding().encodeToString(authBytes);
  24.  
  25.         try {
  26.             URL url = new URL("http://192.168.0.25:8080"+apiCall);
  27.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  28.  
  29.             connection.setRequestMethod("GET");
  30.             connection.setRequestProperty("Authorization", "Basic " + encoded);
  31.             connection.setDoOutput(true);
  32.  
  33.             if (connection.getResponseCode() == 200 ) {
  34.                 BufferedReader out = new BufferedReader(
  35.                         new InputStreamReader(connection.getInputStream()));
  36.                 String inpuLine;
  37.                 while ((inpuLine = out.readLine()) != null) {
  38.                     this.response.append(inpuLine);
  39.                 }
  40.                 out.close();
  41.                 this.output = this.response.toString();
  42.  
  43.             }
  44.             else
  45.                 System.out.println(connection.getResponseCode());
  46.         } catch (IOException e) {
  47.             e.printStackTrace();
  48.         }
  49.  
  50.     }
  51.  
  52.  
  53.     boolean  restLogin(String username, String password, String apiCall) {
  54.  
  55.         String login = username + ":" + password;
  56.         final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
  57.         String encoded = Base64.getEncoder().withoutPadding().encodeToString(authBytes);
  58.  
  59.         try {
  60.             URL url = new URL("http://192.168.0.25:8080"+apiCall);
  61.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  62.  
  63.             connection.setRequestMethod("GET");
  64.             connection.setRequestProperty("Authorization", "Basic " + encoded);
  65.             connection.setDoOutput(true);
  66.  
  67.             if (connection.getResponseCode() == 200 ) {
  68.                 System.out.println("Witaj " + username);
  69.             }
  70.             else{
  71.                 System.out.println("Logowanie nieudane, kod błędu:" + connection.getResponseCode());
  72.                 return false;
  73.             }
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.         }
  77.  
  78.         return true;
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement