码迷,mamicode.com
首页 > 编程语言 > 详细

java httpclient 跳过证书验证

时间:2016-08-03 22:01:21      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {

private SSLContext sslContext = null;

/**
* Constructor for MySecureProtocolSocketFactory.
*/
public MySecureProtocolSocketFactory() {
}

/**
* @return
*/
private static SSLContext createEasySSLContext() {
try {
SSLContext context = SSLContext.getInstance("SSL");
context.init(null, new TrustManager[] {new MyX509TrustManager()}, null);
return context;
} catch (Exception e) {
throw new HttpClientError(e.toString());
}
}

/**
* @return
*/
private SSLContext getSSLContext() {
if (this.sslContext == null) {
this.sslContext = createEasySSLContext();
}
return this.sslContext;
}

/*
* (non-Javadoc)
* @see
* org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
* int, java.net.InetAddress, int)
*/
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException,
UnknownHostException {

return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
}

/*
* (non-Javadoc)
* @see
* org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
* int, java.net.InetAddress, int, org.apache.commons.httpclient.params.HttpConnectionParams)
*/
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
if (timeout == 0) {
return createSocket(host, port, localAddress, localPort);
} else {
return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
}
}

/*
* (non-Javadoc)
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
*/
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port);
}

/*
* (non-Javadoc)
* @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
*/
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
}
}

 

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;

public class MyX509TrustManager implements X509TrustManager {

/*
* (non-Javadoc)
* @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[],
* java.lang.String)
*/
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

}

/*
* (non-Javadoc)
* @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[],
* java.lang.String)
*/
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {

}

/*
* (non-Javadoc)
* @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
*/
public X509Certificate[] getAcceptedIssuers() {
return null;
}

}

public static void main(String[] args) {
try {
// System.out.println(ToolUtils.encryptBASE64("cb:20160803010100"));
// System.out.println(ToolUtils.md5("cb201608030101001572874614213670297489123456789"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BidirectionalBean bidbean=new BidirectionalBean();
bidbean.setCallerNumber("15728746142");
bidbean.setCalledNumber("13670297489");
bidbean.setShowCaller("");
bidbean.setUserData("123456789");
bidbean.setSig("3bb3b074d67ae8916cda3899bc7cc7e4");
String responseMsg = "";
//构造httpclient实例
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
HttpClient http=new HttpClient();
//参数的转码格式
http.getParams().setContentCharset("utf-8");
PostMethod post=new PostMethod("https://192.168.1.58:8443/CtdWebCall/api/extension/call");
post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 2000);
post.setRequestHeader("Content-Type", "application/json;charset=utf-8");
post.setRequestHeader("Accept","application/json");
post.setRequestHeader("Authorization","Y2I6MjAxNjA4MDMwMTAxMDA=");
RequestEntity requeste;
try {
requeste = new StringRequestEntity(JSON.toJSONString(bidbean), "application/json", "utf-8");
post.setRequestEntity(requeste);
http.executeMethod(post);
if(post.getStatusCode()==HttpStatus.SC_OK){//200http请求返回成功
//请求返回的参数,第一种方式
responseMsg=post.getResponseBodyAsString().trim();
System.out.println(responseMsg);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(post!=null){
//关闭请求连接
post.releaseConnection();
}
}
}

 

java httpclient 跳过证书验证

标签:

原文地址:http://www.cnblogs.com/cbdd/p/5734223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!