Java HttpsURLConnection create

Description

Java HttpsURLConnection create


import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Main {

   public static void main(String[] args) throws Exception {
      // configure the SSLContext with a TrustManager
      SSLContext ctx = SSLContext.getInstance("TLS");
      ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
      SSLContext.setDefault(ctx);

      URL url = new URL("https://demo2s.com");
      HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
      conn.setHostnameVerifier(new HostnameVerifier() {
         @Override//  w ww. j ava 2 s .c  o  m
         public boolean verify(String arg0, SSLSession arg1) {
            return true;
         }
      });
      System.out.println(conn.getContent());
      conn.disconnect();
   }

   private static class DefaultTrustManager implements X509TrustManager {

      @Override
      public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
      }

      @Override
      public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
      }

      @Override
      public X509Certificate[] getAcceptedIssuers() {
         return null;
      }
   }
}



PreviousNext

Related