Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package hu.rexpect.test;
  2.  
  3. import java.net.MalformedURLException;
  4. import java.net.URI;
  5. import java.net.URISyntaxException;
  6. import java.net.URL;
  7. import java.security.GeneralSecurityException;
  8. import java.security.KeyManagementException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.SecureRandom;
  11. import java.security.cert.Certificate;
  12. import java.security.cert.CertificateException;
  13. import java.io.*;
  14.  
  15. import javax.net.ssl.HostnameVerifier;
  16. import javax.net.ssl.HttpsURLConnection;
  17. import javax.net.ssl.SSLContext;
  18. import javax.net.ssl.SSLPeerUnverifiedException;
  19. import javax.net.ssl.SSLSession;
  20. import javax.net.ssl.TrustManager;
  21. import javax.net.ssl.X509TrustManager;
  22. import javax.security.cert.X509Certificate;
  23.  
  24. import org.apache.http.client.utils.URIBuilder;
  25.  
  26. public class HttpsTest {
  27.  
  28.     public void testIt() {
  29.        
  30.         /*try {
  31.             SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  32.             SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("xy.zzs.hu", 443);
  33.  
  34.             InputStream in = sslsocket.getInputStream();
  35.             OutputStream out = sslsocket.getOutputStream();
  36.  
  37.             // Write a test byte to get a reaction :)
  38.             out.write(1);
  39.  
  40.             while (in.available() > 0) {
  41.                 System.out.print(in.read());
  42.             }
  43.             System.out.println("Successfully connected");
  44.  
  45.         } catch (Exception exception) {
  46.             exception.printStackTrace();
  47.         }
  48.    
  49.         System.out.println("\n********************************");*/
  50.        
  51.         URL url = null;
  52.  
  53.         try {
  54.             URI uri = new URIBuilder()
  55.                     .setScheme("https")
  56.                     .setHost("xy.zzs.hu")
  57.                     .setPath("/eleres/Cuccli")
  58.                     .setParameter("action", "verify")
  59.                     .setParameter("AMOUNT", "101")
  60.                     .build();
  61.  
  62.             url = uri.toURL();
  63.             System.out.println(url);
  64.         } catch (URISyntaxException e1) {
  65.             // TODO Auto-generated catch block
  66.             e1.printStackTrace();
  67.         } catch (MalformedURLException e) {
  68.             // TODO Auto-generated catch block
  69.             e.printStackTrace();
  70.         }
  71.  
  72.        
  73.  
  74.         try {
  75.             url = new URL(https_url);
  76.         } catch (MalformedURLException e1) {
  77.             // TODO Auto-generated catch block
  78.             e1.printStackTrace();
  79.         }*/
  80.        
  81.         try {
  82.  
  83.             HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  84.  
  85.             print_https_cert(con);
  86.  
  87.             print_content(con);
  88.  
  89.         } catch (MalformedURLException e) {
  90.             e.printStackTrace();
  91.         } catch (IOException e) {
  92.             e.printStackTrace();
  93.         }
  94.  
  95.     }
  96.  
  97.     private void print_https_cert(HttpsURLConnection con) {
  98.  
  99.         if (con != null) {
  100.  
  101.             try {
  102.  
  103.                 System.out.println("Response Code : " + con.getResponseCode());
  104.                 System.out.println("Cipher Suite : " + con.getCipherSuite());
  105.                 System.out.println("\n");
  106.  
  107.                 Certificate[] certs = con.getServerCertificates();
  108.                 for (Certificate cert : certs) {
  109.                     System.out.println("Cert Type : " + cert.getType());
  110.                     System.out.println("Cert Hash Code : " + cert.hashCode());
  111.                     System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
  112.                     System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
  113.                     System.out.println("\n");
  114.                 }
  115.  
  116.             } catch (SSLPeerUnverifiedException e) {
  117.                 e.printStackTrace();
  118.             } catch (IOException e) {
  119.                 e.printStackTrace();
  120.             }
  121.  
  122.         }
  123.  
  124.     }
  125.  
  126.     private void print_content(HttpsURLConnection con) {
  127.         if (con != null) {
  128.  
  129.             try {
  130.  
  131.                 System.out.println("****** Content of the URL ********");
  132.                 BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  133.  
  134.                 String input;
  135.  
  136.                 while ((input = br.readLine()) != null) {
  137.                     System.out.println(input);
  138.                 }
  139.                 br.close();
  140.  
  141.             } catch (IOException e) {
  142.                 e.printStackTrace();
  143.             }
  144.  
  145.         }
  146.  
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement