create SSL Context - Java Network

Java examples for Network:SSL

Description

create SSL Context

Demo Code


import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

public class Main{
    public static void main(String[] argv) throws Exception{
        System.out.println(createSSLContext());
    }//w  w w. j  av  a  2s. c o m
    public static SSLContext createSSLContext()
            throws GeneralSecurityException {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(
                null,
                new X509TrustManager[] { new CertificateAuthorityTrustManager() },
                new SecureRandom());
        return context;
    }
}

Related Tutorials