httpclient调用https
httpclient调用https报错:
Exception in thread "main" java.lang.Exception: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
代码:
package com.taiping; import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Map; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol; import com.taiping.base.facility.tool.JAXBTool;
import com.taiping.base.util.IOTool;
import com.taiping.partner.antgroup.DTO.ali.request.RequestCheckDoc;
import com.taiping.partner.antgroup.DTO.ali.request.RequestCheckbillDoc;
import com.taiping.z.HTTPSSecureProtocolSocketFactory; public class HttpXmlSender_189_1_LOCAL
{
public static void main(String[] args) throws Exception
{
// 核保:"test-antgroup-underwrite-12602-5.xml";
// 出单:"test-antgroup-issue-12602-5.xml"; test-antgroup-issue-EBB.xml test-antgroup-issue-YaZhouLYYW.xml
String file = "test-antgroup-underwrite-EBB.xml"; String path = HttpXmlSender_189_1_LOCAL.class.getResource("/template/"+file).getPath();
System.out.println("path : "+path); // 请确认编码方式
String encode = "utf-8";
String xml = IOTool.readFile(path, encode); //请确认编码方式 // localhost本地
// String url = "http://localhost:8000/taiping-sol-mall/services/antgroup/entrance";
// UAT环境
String url = "https://baoxian.itaiping.com/services/antgroup/entrance";
// String url = "http://10.1.17.95:7002/services/antgroup/entrance"; // 生产环境
// String url = "http://baoxian.cntaiping.com/partner/partnerEntrance.action";
// 生产环境IP访问
// String url = "http://10.4.233.6:8003/partner/partnerEntrance.action"; String policyNo ="2016ANTGROUP_XLH08171";
xml = xml.replaceAll("#policyNo#", policyNo); // 1、解析报文
RequestCheckDoc requestCheckDoc = null;
RequestCheckbillDoc requestCheckbillDoc = null;
try {
if (xml.indexOf("underwrite") > -) {
requestCheckDoc = (RequestCheckDoc) JAXBTool.unmarshal(xml, RequestCheckDoc.class);
requestCheckDoc.setSignature();
xml = JAXBTool.marshal(requestCheckDoc);
}else {
requestCheckbillDoc = (RequestCheckbillDoc) JAXBTool.unmarshal(xml, RequestCheckbillDoc.class);
requestCheckbillDoc.setSignature();
xml = JAXBTool.marshal(requestCheckbillDoc);
}
System.out.println("requestXml:\n"+xml);
} catch (Exception e) {
e.printStackTrace();
} String responseXml = new HttpXmlSender_189_1_LOCAL().post(url,xml,encode,policyNo);
System.out.println("responseXml:\n"+responseXml);
} /**
* post 方法
* @param url
* @param params
* @return
*/
public String post(String url, Object content, String encode,String policyNo) throws Exception { String responseMsg = "";
byte[] responseBody = null;
HttpClient httpclient = new HttpClient(); // 为支持https step1
Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", https); PostMethod httpPost = new PostMethod(url);
httpPost.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
httpPost.setRequestHeader("ContentType", "application/xml;charset=GBK");
// 设置连接超时时间(单位毫秒)
httpclient.getHttpConnectionManager().getParams().setConnectionTimeout();
// 设置读数据超时时间(单位毫秒)
httpclient.getHttpConnectionManager().getParams().setSoTimeout();
try {
httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(, false));
// servlet
if (content instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, String> map = (Map<String, String>)content;
NameValuePair[] param = new NameValuePair[map.size()]; int index = ;
for (Map.Entry<String, String> entry : map.entrySet()) {
param[index] = new NameValuePair(entry.getKey(),URLEncoder.encode(entry.getValue(), "GBK"));
} httpPost.setRequestBody(param);
}
// rest
else {
httpPost.setRequestEntity(new StringRequestEntity((String)content,"application/xml", encode));
// System.out.println("=========================================================");
// System.out.println("request by rest ......");
} // post
int statusCode = httpclient.executeMethod(httpPost);
// 为支持https step1 over
Protocol.unregisterProtocol("https"); System.out.println("policyNo:"+policyNo+",statusCode:"+statusCode);
// success
if (statusCode == HttpStatus.SC_OK) {
responseBody = httpPost.getResponseBody();
}
// failure
else {
responseMsg = String.valueOf("statusCode:"+statusCode);
}
} catch (HttpException e) {
throw new Exception(e.getMessage());
} catch (IOException e) {
throw new Exception(e.getMessage());
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
httpPost.releaseConnection();
} if (responseBody != null) {
responseMsg = new String(responseBody, encode);
}
return responseMsg;
}
}
HTTPSSecureProtocolSocketFactory:
package com.taiping.z;
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.ProtocolSocketFactory; /**
* httpclient https
*
*/
public class HTTPSSecureProtocolSocketFactory implements ProtocolSocketFactory {//SecureProtocolSocketFactory
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 (null == this.sslcontext) {
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 == ) {
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[] {};
}
} }
httpclient调用https的更多相关文章
- java 通过httpclient调用https 的webapi
java如何通过httpclient 调用采用https方式的webapi?如何验证证书.示例:https://devdata.osisoft.com/p...需要通过httpclient调用该接口, ...
- 在 IIS 6 和 IIS 7中配置Https,设置WCF同时支持HTTP和HTPPS,以及使用HttpWebRequest和HttpClient调用HttpS
IIS 7 ,给IIS添加CA证书以支持https IIS 6 架设证书服务器 及 让IIS启用HTTPS服务 WCF IIS 7中配置HTTPS C#利用HttpWebRequest进行post请求 ...
- Java 使用 HttpClient调用https 最新源码 JDK7+ apache4.3+
在项目使用https方式调用别人服务的时候,以前要写很多的代码,现在框架封装了很多,所以不用再写那么多了. 网上看了一下,都是很老的版本,用过时的DefaultHttpClient. 以spring为 ...
- Java调用Http/Https接口(4)--HttpClient调用Http/Https接口
HttpClient是Apache HttpComponents项目下的一个组件,是Commons-HttpClient的升级版,两者api调用写法也很类似.文中所使用到的软件版本:Java 1.8. ...
- 使用HttpClient携带pfx证书调用HTTPS协议的WebService
调用第三方服务时,厂商提供了一个WSDL文件.调用的地址和一个后缀为pfx的证书文件,通过SOUPUI记载证书是可以正常调用WebService服务,那么如何将该服务转换为代码呢? 咨询了厂商的支持, ...
- [转]java 关于httpclient 请求https (如何绕过证书验证)
原文:http://www.blogjava.net/hector/archive/2012/10/23/390073.html 第一种方法,适用于httpclient4.X 里边有get和post两 ...
- 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn't match any of the subject alternative names问题的解决
最近用server酱-PushBear做消息自动推送,用apache HttpClient做https的get请求,但是代码上到服务器端就报javax.net.ssl.SSLException: Ce ...
- Httpclient 支持https(转)
参考:https://jingyan.baidu.com/article/154b46317353d228ca8f4112.html 参考:https://www.jianshu.com/p/a444 ...
- 关于httpclient 请求https (如何绕过证书验证)
第一种方法,适用于httpclient4.X 里边有get和post两种方法供你发送请求使用.导入证书发送请求的在这里就不说了,网上到处都是 import java.io.BufferedReader ...
随机推荐
- python中的进程、线程(threading、multiprocessing、Queue、subprocess)
Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...
- hdu 小希的迷宫
真是被这道并查集的水题给坑哭了...忘记给uset数组初始化我也是醉了. 题目意思是找到判断是不是连通无环的图,首先想到的就是并查集. 1判断成环的时候,只要判断输入边的两个点.有一个共同的父节点,那 ...
- WPF 程序启动显示为通知区域的图标方法
首先需要引用 System.Windows; System.Drawing; public partial class MainWindow : Window { public MainWindo ...
- 从客户端(Content="<p>SF</p>")中检测到有潜在危险的 Request.Form 值
出现这个问题,一般是由于.Net Framework 使用了4.0或以上版本造成的.(从 .Net Framework 4.0 开始,ASP.NET开始强制检测Request参数安全),而我们可以通过 ...
- 演示对sys用户和普通用户进行审计的示例
1.确认数据库版本 1对SYS用户审计 1.1配置审计参数 1.2修改liunx日志配置文件 添加以下一列: 1.3 SYS 用户操作演示 2对普通用户审计 2.1配置审计参数 2.2演示对TEST用 ...
- Linux常用命令(持续更新)
lsb_release -a 查看linux操作系统信息 getconf LONG_BIT 查看linux操作系统位数 useradd [-g groupname] username 创建用户,并指定 ...
- centos6 LVS-DR模式---分析
LVS是什么就不多说了. 先上拓扑图 1台LVS 3台Realserver 一个客户端. 环境全部模拟全在内网环境(selinux和iptables关闭) 先简略说一下安装步骤: LVS上 ...
- 基于Jenkins的环境搭建
基于 Jenkins 快速搭建持续集成环境 持续集成是一种软件开发实践,对于提高软件开发效率并保障软件开发质量提供了理论基础.Jenkins 是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续 ...
- 使用花生壳6.5客户端FTP设置
1.打开FTP客户端—选项—参数选择 2.设置为主动模式(PORT) 3.连接FTP服务器 4.FTP连接成功
- 第一课JAVA开发环境配置
进行JAVA环境安装首先得进行jdk1.7部署,注意应放在没有中文和空格的目录下,然后进行配置环境变量,配置环境变量分为三步: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_ ...