https需要的类

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);

时间: 2024-10-16 17:19:36

https需要的类的相关文章

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

下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEn

Https通信工具类

记录一个在微信开发中用到的https通信工具类,以后会用到的. 用于https通信的证书信任管理器 import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; /** * 证书信任管理器(用于https请求) */ public class MyX509TrustManager implements

Https联网工具类

Https联网工具类 get  post 调用时 只需传入url,和要提交的参数queryString  有cookie时也可以传入 放回的是字符串 连接方式我在Http里解决了你们只需要拼接对 拼接格式 路径:https://emall.licaike.com/weixin/web/bind?        参数:loginInit=loginInit&knowChannel=APP_LCK_ADR_KC import java.io.ByteArrayOutputStream; import

https请求工具类

import java.io.IOException;import java.io.UnsupportedEncodingException;import java.lang.reflect.Field;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java

C# 模拟Http/Https请求框架类

using System.Text;using System.Net;using System.IO;using System.Text.RegularExpressions;using System.Security.Cryptography.X509Certificates;using System.Net.Security; namespace WebRequestTest{    /// <summary>    /// 动态类,每个实例使用单独session    /// </

httpclient4.3访问https

1.创建一个访问https的工具类 package org.aaa.tool;import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; im

Android 开源框架Universal-Image-Loader加载https图片

解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostra13.universalimageloader.core.download下面的BaseImageDownloader这个类 里面的createConnection这个方法,添加如下: 1 protected HttpURLConnection createConnection(String ur

HTTPS接口获取数据报错的问题

local_policy.jar&US_export_policy.jar 因为项目需要,所以写一个https的工具类. 写完之后,在一些开放平台中找了一些https接口,我是在京东万象中找的. 测试完这些公网的接口之后,看起来都是完全OK,没问题的. 在上海一个地方有同事进行项目实施的时候,用到这个工具类,一直报错,后来去了现场,经过一系列改动之后,就是一直报一个错误, 后来查了一下,可能是jdk自带的这两个jar包导致的,具体原因就是 因为某些国家的进口管制限制,Java发布的运行环境包中的

springboot添加https

一.使用JDK工具keytool生成证书 keytool命令详解 https://blog.csdn.net/zlfing/article/details/77648430 keytool -genkey -alias tomcat -keypass password -storetype PKCS12 -keyalg RSA -keysize 2048 -validity 3650 -keystore keystore.p12 -storepass password -dname "CN=(A