集册 Java实例教程 获取SSL HTTP连接

获取SSL HTTP连接

欢马劈雪     最近更新时间:2020-01-02 10:19:05

740
获取SSL HTTP连接
/**
nowjava.com 提供 
**/

/*******************************************************************************

 * 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:

 *     IBM Corporation - initial API and implementation

 *******************************************************************************/

//package com.nowjava;


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.g
展开阅读全文