import java.io.IOException;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.net.SocketAddress;

import java.net.UnknownHostException;

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.SocketFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;

import org.apache.commons.httpclient.params.HttpConnectionParams;

import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

public class MySSLSocketFactory implements SecureProtocolSocketFactory {
static{
     System.out.println("loading SSL");
}
private SSLContext sslcontext = null;
 
private SSLContext createSSLContext() {
     SSLContext sslcontext=null;
     try {
         sslcontext = SSLContext.getInstance("SSL");
         sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
     } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
     } catch (KeyManagementException e) {
         e.printStackTrace();
     }
     return sslcontext;
}
 
private SSLContext getSSLContext() {
     if (this.sslcontext == null) {
         this.sslcontext = createSSLContext();
     }
     return this.sslcontext;
}
 
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
         throws IOException, UnknownHostException {
     return getSSLContext().getSocketFactory().createSocket(
             socket,
             host,
             port,
             autoClose
         );
}

public Socket createSocket(String host, int port) throws IOException,
         UnknownHostException {
     return getSSLContext().getSocketFactory().createSocket(
             host,
             port
         );
}
 
 
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
         throws IOException, UnknownHostException {
     return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
}

public Socket createSocket(String host, int port, InetAddress localAddress,
         int localPort, HttpConnectionParams params) throws IOException,
         UnknownHostException, ConnectTimeoutException {
     if (params == null) {
         throw new IllegalArgumentException("Parameters may not be null");
     }
     int timeout = params.getConnectionTimeout();
     SocketFactory socketfactory = getSSLContext().getSocketFactory();
     if (timeout == 0) {
         return socketfactory.createSocket(host, port, localAddress, localPort);
     } else {
         Socket socket = socketfactory.createSocket();
         SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
         SocketAddress remoteaddr = new InetSocketAddress(host, port);
         socket.bind(localaddr);
         socket.connect(remoteaddr, timeout);
         return socket;
     }
}
 
//自定义私有类
private static class TrustAnyTrustManager implements X509TrustManager {
     public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
     }
     public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
     }
     public X509Certificate[] getAcceptedIssuers() {
         return new X509Certificate[]{};
     }
}

}

Protocol myhttps = new Protocol("https", new MySSLSocketFactory (), 443);
         Protocol.registerProtocol("https", myhttps);

https需要的类的更多相关文章

  1. Http、Https请求工具类

    最近在做微信开发,使用http调用第三方服务API,有些是需要https协议,通过资料和自己编码,写了个支持http和https的工具类,经验证可用,现贴出来保留,也供需要的人使用(有不足的地方,也请 ...

  2. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...

  3. 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

    下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...

  4. Https通信工具类

    记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; im ...

  5. Java 发送 Https 请求工具类 (兼容http)

    依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...

  6. HttpClient发起Http/Https请求工具类

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  7. Java 实现Https访问工具类 跳过ssl证书验证

    不多BB ,代码直接粘贴可用 import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.F ...

  8. 在Spring Boot中使用Https

    本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...

  9. RestTemplate发送HTTP、HTTPS请求

    RestTemplate 使用总结   场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...

随机推荐

  1. oracle中修改表名

    <<<备忘>>>   answer1: ALTER TABLE old_table_name RENAME TO new_table_name;(大写为系统命令) ...

  2. css 颜色渐变

    .top_nav {     width: 100%;     height: 29px;            /* 如果浏览器不支持渐变,使用图像作为背景 */     background: u ...

  3. hiho_1087_哈密顿环

    题目 在一个有向图上,从一点A出发,经过所有除A的顶点一次且仅经过一次,最后到达起始点A,所形成的路径为哈密顿环.两个哈密顿环不同,当且仅当路径上的任意一个顶点P的下一个顶点不同.     给出一个顶 ...

  4. intel simd 资料

    http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.html https://software.intel.com/sites/landingp ...

  5. git服务器搭建-new

    http://blog.chinaunix.net/uid-26729093-id-4652536.html 基本命令使用:http://www.cnblogs.com/xdao/p/linux_gi ...

  6. BOM头的来源

    类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...

  7. 设置westorm自动代码提示

    打开settings 然后在js文件下 打出co  按TAB键就出现了color了

  8. Qt之Concurrent Map和Map-Reduce

    简述 QtConcurrent::map().QtConcurrent::mapped()和QtConcurrent::mappedReduced()函数在一个序列中(例如:QList或QVector ...

  9. thinkPHP开发基础知识 包括变量神马的

    hinkPHP框架开发的应用程序,一般都采用单一入口的方式,下面是在应用首页文件中实现的定义: 1.在首页定义thinkPHP框架路径 2.定义项目名称及路径,一般项目名称与项目文件夹名称保持一致 3 ...

  10. SqlServer中把结果集放到到临时表的方法

    一. SELECT INTO   1. 使用select into会自动生成临时表,不需要事先创建   select * into #temp from sysobjects   01. 把存储过程结 ...