该问题的解决办法   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. Struts2将图片输出到页面

            在做CRUD的过程中,添加页面是个表单,表单里面有一项是上传头像文件.这样表单提交后,头像文件上传了. 但这个文件存的地址是本地硬盘的一个文件夹.在编辑页面要做这个头像的回显的话,就需 ...

  2. 解决debug到jdk源码时不能查看变量值的问题

    目录 如何跟踪jdk源码 1. 编译源码 2. 关联源码 3. 大功告成 如何跟踪jdk源码 看到这个标题大概大家都会在心里想谁还跟踪个源码呀,在eclipse中打个断点,以debug的方式运行,然后 ...

  3. 开源软件License汇总

    用到的open source code越多,遇到的开源License协议就越多.License是软件的授权许可,里面详尽表述了你获得代码后拥有的权利,可以对别人的作品进行何种操作,何种操作又是被禁止的 ...

  4. PHP7添加opcache.so模块

    启动php报错如下: # /usr/local/php7/sbin/php-fpm [-Apr- ::] NOTICE: PHP message: PHP Warning: PHP Startup: ...

  5. sqlyog mysql 外键引用列找不到想要的字段的原因

    这是因为引用列必须为一个主键才行

  6. [CF963E]Circles of Waiting[高斯消元网格图优化+期望]

    题意 你初始位于 \((0,0)\) ,每次向上下左右四个方向走一步有确定的概率,问你什么时候可以走到 以 \((0,0)\)为圆心,\(R\) 为半径的圆外. \(R\le 50\) 分析 暴力 \ ...

  7. alpha发布排序结果

    友组所做排序 其中有一组是教师排序. 序号 组名 组长 项目简称 匿名1组 匿名2组 匿名3组 匿名4组 匿名5组 匿名6组 匿名7组 匿名8组 平均 1 新蜂 武志远 俄罗斯 2 3 3 4 4 5 ...

  8. Linux第一章读书笔记

    一.Linux历史 Unix强大的根本原因: 1.简洁,仅仅提供几百个系统调用并且有一个非常明确的设计目的 2.文件对待所有东西,通过一套相同的系统调用接口来进行对数据和设备的操作 3.由于用C语言编 ...

  9. c# 导出数据到excel

    直接上代码: private void button1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButton ...

  10. C++的OOP特性

    内存模型和名称空间 存储持续性,作用域和链接性 C++有三种方案来存储数据 自动存储持续性:在函数定义中声明的变量,包括函数参数.在函数或代码块开始执行时创建.执行完函数或者代码块,内存自动释放. 静 ...