该问题的解决办法   1、在请求前需要将证书导入,不推荐       2、绕开安全协议处理

下面的代码时一段http请求并且绕开安全协议。可直接使用

/**
*
* @param url 需要请求的网关路径
* @param sendData 请求时需要传入的参数
* @param urlencode url的编码格式
* @param connTimeOut 链接超时时间
* @param readTimeOut 读取超时时间
* @param contentType 请求头部 固定输入"application/x-www-form-urlencoded;charset="+urlencode
* @param header 输入null
* @return
*/
public static String sendAndRcvHttpPostBase(String url,String sendData,String urlencode,int connTimeOut,int readTimeOut,String contentType,Map<String,String> header){
Long curTime = System.currentTimeMillis();
Trace.logInfo(Trace.COMPONENT_HTTP, "SimpleHttpConnUtil Prepare @"+curTime);
String result = "";
BufferedReader in = null;
DataOutputStream out = null;
int code = 999;
HttpsURLConnection httpsConn = null;
HttpURLConnection httpConn = null;
try{
URL myURL = new URL(url);
Trace.logInfo(Trace.COMPONENT_HTTP, "请求地址:"+url);
if(url.startsWith("https://")){
httpsConn = (HttpsURLConnection) myURL.openConnection();
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
httpsConn.setSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
@Override
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
httpsConn.setHostnameVerifier(hv); httpsConn.setRequestProperty("Accept-Charset", urlencode);
httpsConn.setRequestProperty("User-Agent","java HttpsURLConnection");
if(header!=null){
for(String key:header.keySet()){
httpsConn.setRequestProperty(key, (String)header.get(key));
}
}
httpsConn.setRequestMethod("POST");
httpsConn.setUseCaches(false);
httpsConn.setRequestProperty("Content-Type",contentType);
httpsConn.setConnectTimeout(connTimeOut);
httpsConn.setReadTimeout(readTimeOut);
httpsConn.setDoInput(true);
httpsConn.setInstanceFollowRedirects(true);
if(sendData !=null){
httpsConn.setDoOutput(true);
// 获取URLConnection对象对应的输出流
out = new DataOutputStream(httpsConn.getOutputStream());
// 发送请求参数
out.write(sendData.getBytes(urlencode));
// flush输出流的缓冲
out.flush();
out.close();
}
// 取得该连接的输入流,以读取响应内容
in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(),urlencode));
code = httpsConn.getResponseCode();
}else{
httpConn = (HttpURLConnection) myURL.openConnection();
httpConn.setRequestProperty("Accept-Charset", urlencode);
httpConn.setRequestProperty("user-agent","java HttpURLConnection");
if(header!=null){
for(String key:header.keySet()){
httpConn.setRequestProperty(key, (String)header.get(key));
}
}
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Content-Type",contentType);
httpConn.setConnectTimeout(connTimeOut);
httpConn.setReadTimeout(readTimeOut);
httpConn.setDoInput(true);
httpConn.setInstanceFollowRedirects(true);
if(sendData !=null){
httpConn.setDoOutput(true);
// 获取URLConnection对象对应的输出流
out = new DataOutputStream(httpConn.getOutputStream());
// 发送请求参数
out.write(sendData.getBytes(urlencode));
// flush输出流的缓冲
out.flush();
out.close();
}
// 取得该连接的输入流,以读取响应内容
in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),urlencode));
code = httpConn.getResponseCode();
}
if (HttpURLConnection.HTTP_OK == code){
String line;
while ((line = in.readLine()) != null) {
result += line; System.out.println("=====反回结果====="+ line); }
if(result.length()>2000){
Trace.logInfo(Trace.COMPONENT_ACTION, "http返回结果 !\n"+result.substring(0,2000)+"...");
}else{
Trace.logInfo(Trace.COMPONENT_ACTION, "http返回结果 !\n"+result);
}
}else{
result = null;
throw new Exception("支付失败,服务端响应码:"+code);
}
}catch(IOException e){
Trace.logError(Trace.COMPONENT_ACTION, "http通讯失败 !",e);
result = null;
}catch(Exception e){
Trace.logError(Trace.COMPONENT_ACTION, "http通讯失败 !",e);
result = null;
}finally{
Trace.logInfo(Trace.COMPONENT_ACTION,"对方地址:"+url);
if(out!=null){
try {
out.close();
} catch (IOException e) {
}
}
if(httpConn!=null){
httpConn.disconnect();
}
if(httpsConn!=null){
httpsConn.disconnect();
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
}
}
}
Trace.logInfo(Trace.COMPONENT_HTTP, "SimpleHttpConnUtil "+curTime+" end for "+(System.currentTimeMillis()-curTime)+"ms");
return result;
}

以上代码中使用的java类的包路径,只有涉及到安全协议的包路径。

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

关于http请求时 安全协议问题 PKIX path building failed 解决办法的更多相关文章

  1. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  2. 解决PKIX path building failed的问题

    Java在请求某些不受信任的https网站时会报:PKIX path building failed 解决方法一:使用keytool手动导入证书,为JRE环境导入信任证书 参考:http://www. ...

  3. 解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target

    最近在写java的一个服务,需要给远程服务器发送post请求,认证方式为Basic Authentication,在请求过程中出现了 PKIX path building failed: sun.se ...

  4. 【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException

    问题报错 今天上线了我开发的一个OAuth2单点登录客户端的实现,在测试系统验证没问题,到生产环境由于单点登录服务端HTTPS协议,报错如下: I/O error on POST request fo ...

  5. java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...

  6. 从头解决PKIX path building failed

    从头解决PKIX path building failed的问题 本篇涉及到PKIX path building failed的原因和解决办法(包括暂时解决和长效解决的方法),也包括HTTP和HTTP ...

  7. 彻底弄懂“PKIX path building failed”问题

    SSL的基础知识 SSL的全称是Secure Socket Layer.它的通信流程如下图所示,客户端与服务端会通过几次通信,通过非对称加密创建出一个加密密钥,用于以后的对称信息加密. 1,客户端明文 ...

  8. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  9. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...

随机推荐

  1. 20155320 Exp3 免杀原理与实践

    20155320 Exp3 免杀原理与实践 免杀 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. [基础问题回答] (1)杀软是如何检测出恶意代码的? 1.通过行为检 ...

  2. TreeSet排序相关总结

            java的集合这一块在工作中用得还比较多,有些东西老是忘,因此在此记录下来. TreeSet原理 1.特点 TreeSet是用来排序的, 可以指定一个顺序, 对象存入之后会按照指定的顺 ...

  3. Kubernetes学习之路(二十一)之网络模型和网络策略

    目录 Kubernetes的网络模型和网络策略 1.Kubernetes网络模型和CNI插件 1.1.Docker网络模型 1.2.Kubernetes网络模型 1.3.Flannel网络插件 1.4 ...

  4. apache目录别名

    #默认家目录DocumentRoot "/var/www/html"<Directory "/var/www"> AllowOverride Non ...

  5. SpringBoot日记——MQ消息队列整合(一)

    除了之前讲到的缓存,我们还会用到消息队列来存储一些消息,为了提升系统的异步性能等等: 消息服务有两个概念需要知道:消息代理-message broker,目的地-destination.消息发送由代理 ...

  6. Sqlserver_分组

    create table orders ( id int, orderName varchar() ) go create table cars ( id ,) , ordersid int, pna ...

  7. SPIR-V*:面向 OpenCL™ 工作负载的英特尔® 显卡编译器默认接口

    英特尔® 显卡编译器最近从 SPIR* 转换到 SPIR-V*,作为面向 OpenCL™ 工作负载的中间表示.这看起来像编译器的内部变化,对用户来说不可见,但是这展示了我们支持 Khronos* 开放 ...

  8. codeforces 1141G Privatization of Roads in Treeland

    题目链接:http://codeforces.com/contest/1141/problem/G 题目大意: 给你一个无向连通图.每条边都有颜色,如果存在一个点的临边中有超过两条边颜色相同,这个点就 ...

  9. coinmarketcap前20之cardano卡尔达诺(ADA艾达币)

    1. 在开始讲述cardano前,我先说说自己在coinmarketcap前20系列的"学习方法". 最初,我把前20做了一个简单表格,不做任何功课的基础上,记录自己对它们的简要认 ...

  10. 20135202闫佳歆--week1 计算机是如何工作的

    计算机是如何工作的 这一周我学习了计算机工作的相关知识. 最基础的,就是冯诺依曼体系结构结构,它最核心的思想是存储程序计算机,要点是:数字计算机的数制采用二进制:计算机应该按照程序顺序执行. 除了思想 ...