Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package pruebas;
  2.    
  3.     import java.net.MalformedURLException;
  4.     import java.net.URL;
  5.     import java.security.cert.Certificate;
  6.     import java.io.*;
  7.     import javax.net.ssl.HttpsURLConnection;
  8.     import javax.net.ssl.SSLPeerUnverifiedException;
  9.  
  10.     public class HttpsClient {
  11.  
  12.     public static void main(String[] args) throws Exception {
  13.         new HttpsClient().testIt();
  14.     }
  15.  
  16.     private void testIt() throws Exception {
  17.  
  18.         String https_url = "https://dominioproveedor.com/get-token/";
  19.         URL url;
  20.         try {
  21.  
  22.             url = new URL(https_url);
  23.             HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  24.             con.setRequestProperty("Authorization", "Basic 1234567891234567891234567890");
  25.  
  26.             print_https_cert(con);
  27.  
  28.             print_content(con);
  29.  
  30.         } catch (MalformedURLException e) {
  31.             e.printStackTrace();
  32.         } catch (IOException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.     }
  37.  
  38.     private void print_https_cert(HttpsURLConnection con) {
  39.  
  40.         if (con != null) {
  41.  
  42.             try {
  43.  
  44.                 System.out.println("Response Code : " + con.getResponseCode());
  45.                 System.out.println("Cipher Suite : " + con.getCipherSuite());
  46.                 System.out.println("\n");
  47.  
  48.                 Certificate[] certs = con.getServerCertificates();
  49.                 for (Certificate cert : certs) {
  50.                     System.out.println("Cert Type : " + cert.getType());
  51.                     System.out.println("Cert Hash Code : " + cert.hashCode());
  52.                     System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
  53.                     System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
  54.                     System.out.println("\n");
  55.                 }
  56.  
  57.             } catch (SSLPeerUnverifiedException e) {
  58.                 e.printStackTrace();
  59.             } catch (IOException e) {
  60.                 e.printStackTrace();
  61.             }
  62.  
  63.         }
  64.  
  65.     }
  66.  
  67.     private void print_content(HttpsURLConnection con) {
  68.         if (con != null) {
  69.  
  70.             try {
  71.  
  72.                 System.out.println("****** Content of the URL ********");
  73.                 BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  74.  
  75.                 String input;
  76.                 while ((input = br.readLine()) != null) {
  77.                     System.out.println(input);
  78.                 }
  79.                 br.close();
  80.             } catch (IOException e) {
  81.                 e.printStackTrace();
  82.             }
  83.         }
  84.     }
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement