java httpclient 跳过证书验证

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

时间: 2024-10-12 15:48:25

java httpclient 跳过证书验证的相关文章

HttpClient忽略用户证书验证的方法

程序中出了这样的异常:javax.net.ssl.SSLException: hostname in certificate didn't match.异常发生在这行代码上:httpClient.execute(httpGet); 初步判断是证书的问题,准备采用忽略证书的方法解决,但在网上找了半天都没找到靠谱的代码解决,最后还是自己琢磨解决了. 得到一个教训,网上找答案是不靠谱的,因为具体问题需要具体分析,别人的方法不一定能解决你的,一定要自己思考,自己琢磨.在网上最重要的是能找到问题的解决思路

Java.HttpClient绕过Https证书解决方案二

方案2 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.*; import javax.net.ssl.HostnameV

Java.HttpClient绕过Https证书解决方案一

方案1 import javax.net.ssl.*; import java.io.*; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import j

HttpClient 使用证书和跳过证书

引用compile 'org.apache.httpcomponents:httpclient:4.5.10'使用证书 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.uti

Android证书验证存漏洞 开发者身份信息可被篡改(转)

原帖地址:http://bbs.pediy.com/showthread.php?p=1335278#post1335278 近期在国内网易,雷锋网等网站爆出谷歌市场上的索尼官方的备份与恢复应用"Backup and Restore"被黑的消息.新闻显示:目前索尼官方的备份与恢复应用"Backup and Restore"已经被黑客彻底破解:甚至在Google Play商店里该应用的所有权都被黑客修改:目前仍不清楚使用破解修改版本的应用,会否对用户造成损害:建议用户

Java HttpClient

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议. http://hc.apache.org/httpcomponents-client-ga/index.html 版本:httpclient-4.2.jar 1.基本请求 //创建一个客户端 HttpClient client = new DefaultHttpClient(); //创建一个

Axis 1 https(SSL) client 证书验证错误ValidatorException workaround

Axis 1.x 编写的client在测试https的webservice的时候, 由于client 代码建立SSL连接的时候没有对truststore进行设置,在与https部署的webservice 连接会在运行时报出: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath

JAVA: httpclient 详解——第二章;

相对于httpurlconnection ,httpclient更加丰富,也更加强大,其中apache有两个项目都是httpclient,一个是commonts包下的,这个是通用的,更专业的是org.apache.http.包下的,所以我一般用后者: httpclient可以处理长连接,保存会话,重连接,以及请求过滤器,连接重用等等... 下面是测试代码(全部总结来自官方文档,以及翻译) 须要下载核心包:httpclient-4.3.4.jar ,也可在官网下载:http://hc.apache

Retrofit+OKHttp忽略https证书验证

记录这个的原因,是因为很多时候,因为后台配置的证书不正确导致APP访问不到服务器数据,导致影响自身的开发进度.没几行代码,逻辑也清晰,所以下面就直接贴出工具类吧: 1 package huolongluo.yeshen.byw.injection.model; 2 3 import java.security.SecureRandom; 4 import java.security.cert.X509Certificate; 5 6 import javax.net.ssl.HostnameVe