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 跳过证书验证的更多相关文章

  1. java httpclient跳过https证书验证

    httpclien调用skipHttpsUtil得wrapClient方法跳过https证书验证 SkipHttpsUtil  skipHttpsUtil=new SkipHttpsUtil();   ...

  2. java实现跳过证书校验

    /** * 跳过证书效验的sslcontext * @return * @throws Exception */ private static SSLContext createIgnoreVerif ...

  3. Java访问HTTPS时证书验证问题

    为了尽可能避免安全问题,公司的很多系统服务都逐步https化,虽然开始过程会遇到各种问题,但趋势不改.最完美的https应用是能实现双向认证,客户端用私钥签名用服务端公钥加密,服务端用私钥签名客户端都 ...

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

    方案2 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.security.Secur ...

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

    方案1 import javax.net.ssl.*; import java.io.*; import java.net.URL; import java.security.KeyManagemen ...

  6. java客户端验证https连接(忽略证书验证和证书验证两种方式)

    首先根据如下操作生成证书,配置springboot https,生成一个简单的https web服务 https://www.cnblogs.com/qq931399960/p/11889349.ht ...

  7. .net core中引用webservice,并忽略https证书验证

    1.打开vs, 工具-->扩展和更新 下载这个 2. 在admin下右键,添加-->connected service 选择wsdl文件路径,或者服务的url,比如https://**** ...

  8. [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法

    作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...

  9. 轻松把玩HttpClient之配置ssl,采用绕过证书验证实现https

    上篇文章说道httpclient不能直接访问https的资源,这次就来模拟一下环境,然后配置https测试一下.在前面的文章中,分享了一篇自己生成并在tomcat中配置ssl的文章<Tomcat ...

随机推荐

  1. SSIS 事务

    本文摘自:http://www.cnblogs.com/tylerdonet/archive/2011/09/23/2186579.html 在这一个随笔中将介绍在package中如何使用事务来保证数 ...

  2. Eclipse中实现JS代码提示功能

    转发: 用Eclipse写js代码时没有提示,很烦,心累: 找个各种方法以及插件,试了一下,个人感觉AngularJS Eclipse 插件很强,好用,不多说,先装上: 然后重启Eclipse ,右键 ...

  3. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  4. 使用fastadmin的页面跳转模板

    1.效果图 2.修改tp默认跳转模板文件( /thinkphp/tpl/dispatch_jump.tpl ),将文件中的内容全部替换成下面的内容然后保存即可,注意替换语言包和图片路径 {__NOLA ...

  5. CodeForces 723F【DFS瞎搞】

    题意: 给你一幅图,你要用这些边构造一个树, s和t两个节点的度数不能超过ds dt 而且图是保证没有环 思路: 树的性质是:无环(已经保证),无向(保证),连通(还要判断) 首先把S,T点从图里剥离 ...

  6. MecanimControl插件随笔

    ----------------------------------------- 4个animatorController是怎么回事? 分别对应 1镜像动画速度>0 2镜像播放速度<0 ...

  7. 如何使得 python 脚本 不一闪而过

    1.  简单的方法是在最后加上如下语句: os.system("pause") 2. 但是这个不一定有用,原因是可能在之前的代码中发生异常,那么我们看到的效果也是直接一闪而过 办法 ...

  8. rn-splash-screen 启动页 ios

    1.删除iOS工程文件夹下的 LaunchScreen.xib 文件 2. 3.  如果第4步不一样的样式,可以重新走这个图片的步骤就行 4.进入iOS工程的AppDelegate.m中做如下修改: ...

  9. laravel-admin 配置富文本编辑器流程

    laravel-admin默认去除富文本编辑器的,官方也给出了配置方法. 我配置的是wangEditor,本来配置完后就能愉快得使用了,可万万没想到还是有坑的.默认是用base64上传的,也就是数据库 ...

  10. 《统计学习方法》笔记九 EM算法及其推广

    本系列笔记内容参考来源为李航<统计学习方法> EM算法是一种迭代算法,用于含有隐变量的概率模型参数的极大似然估计或极大后验概率估计.迭代由 (1)E步:求期望 (2)M步:求极大 组成,称 ...