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 ...
随机推荐
- css形状大全
转至:http://blog.sina.com.cn/s/blog_4abb9bba0101acsx.html
- .net获取select控件中的文本内容
.net获取select控件中的文本内容 2009-11-28 21:19小V古 | 分类:C#/.NET | 浏览1374次 <select id="SecType" st ...
- soap request by afnetworking2.X/3.X
for 2.X 参考 http://jiapumin.iteye.com/blog/2109378 AFHTTPRequestOperationManager *manager = [AFHTTPRe ...
- python中raw_input输入数字问题
如果按照下面方式,则无论你输入什么,都会打印12,因为raw_input接受的输入是按照字符串处理的 num = raw_input('please enter a num:') if num > ...
- PHP文件操作系统----主要的文件操作函数
一.文件操作系统概述 1.概述: php中的文件操作系统主要是对文件和目录的操作.文件在windows系统下分为3种不同:文件.目录.未知,在linux/unix系统下分为7种不同:block.cha ...
- php发送post请求到nodejs服务器
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); 改为 curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build ...
- 纯CSS实现tooltip提示框,CSS箭头及形状之续篇--给整个tooltip提示框加个边框
在前面一篇中我们介绍了纯CSS实现tooltip提示框,通俗的讲也就是CSS箭头及形状 不过注意一点是,他始终是一个元素,只是通过CSS实现的,今天我们要说的是给这个“tooltip提示框”整体加一个 ...
- linux下tar、zip等压缩、解压命令
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- 加载UI
weak情况 1 2 3 4 @property (weak,nonatomic) UILabel *nameLabel; UILabel *nameLabel = [[UILabel alloc ...
- mongodb配置
Mongodb1. 安装2. CRUD3. 索引4. 副本及(replica sets)5. 分片(sharding) nosql 简单数据模型 元数据和应用数据分离 弱一致性 优势: 避免不必要的复 ...