get SSL HTTP Connection - Java Network

Java examples for Network:SSL

Description

get SSL HTTP Connection

Demo Code

/*******************************************************************************
 * Copyright (c) 2010, 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  w w .jav a 2s. c om
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package com.java2s;

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

public class Main {
    public static HttpsURLConnection getConnection(URL httpsURL) {
        try {
            SSLContext sc = SSLContext.getInstance("TLS"); //$NON-NLS-1$
            sc.init(null, null, new java.security.SecureRandom());
            HttpsURLConnection con = (HttpsURLConnection) httpsURL
                    .openConnection();
            con.setSSLSocketFactory(sc.getSocketFactory());
            return con;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials