关于http请求时 安全协议问题 PKIX path building failed 解决办法
该问题的解决办法 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 解决办法的更多相关文章
- 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法
抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...
- 解决PKIX path building failed的问题
Java在请求某些不受信任的https网站时会报:PKIX path building failed 解决方法一:使用keytool手动导入证书,为JRE环境导入信任证书 参考:http://www. ...
- 解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target
最近在写java的一个服务,需要给远程服务器发送post请求,认证方式为Basic Authentication,在请求过程中出现了 PKIX path building failed: sun.se ...
- 【问题记录】Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException
问题报错 今天上线了我开发的一个OAuth2单点登录客户端的实现,在测试系统验证没问题,到生产环境由于单点登录服务端HTTPS协议,报错如下: I/O error on POST request fo ...
- 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 ...
- 从头解决PKIX path building failed
从头解决PKIX path building failed的问题 本篇涉及到PKIX path building failed的原因和解决办法(包括暂时解决和长效解决的方法),也包括HTTP和HTTP ...
- 彻底弄懂“PKIX path building failed”问题
SSL的基础知识 SSL的全称是Secure Socket Layer.它的通信流程如下图所示,客户端与服务端会通过几次通信,通过非对称加密创建出一个加密密钥,用于以后的对称信息加密. 1,客户端明文 ...
- 解决 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 ...
- javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...
随机推荐
- 2017-2018-2 20155224『网络对抗技术』Exp8:Web基础
实践具体要求 Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前端javascipt(0.5分) 理 ...
- 2017-2018-2 20155233『网络对抗技术』Exp6:信息收集与漏洞扫描
通过DNS和IP挖掘目标网站的信息 whois查询:用来进行域名注册信息查询,以得到3R注册信息,包括注册人的名字.组织.城市等信息.(进行whois查询时去掉www等前缀,因为注册域名时通常会注册一 ...
- Android开发——Android进程保活招式大全
)前台进程(Foreground process),即用户当前操作所必需的进程,通常数量不多.举例如下: //拥有用户正在交互的 Activity(已调用 onResume()) //拥有某个 Ser ...
- Spring MVC统一异常处理
实际上Spring MVC处理异常有3种方式: (1)一种是在Controller类内部使用@ExceptionHandler使用注解实现异常处理: 可以在Controller内部实现更个性化点异常处 ...
- 《Effective Java》学习笔记 —— 序列化
Java的序列化API提供了一个框架,用来将对象编码成一个字节流(序列化,serializing),并从字节流中重新创建对象(反序列化, deserializing). 第74条 谨慎地实现Seria ...
- CSS 背景实例
CSS 背景属性属性 描述background 简写属性,作用是将背景属性设置在一个声明中.background-attachment 背景图像是否固定或者随着页面的其余部分滚动.background ...
- 一种C#泛型方法在lua中表示的设计
在进行lua方法注册的时候, 大多数解决方案直接否定了泛型方法, 因为在lua侧难以表达出泛型, 以及lua的函数重载问题, 函数重载问题可以通过一些特殊方法解决, 而泛型问题是主要问题, 以Unit ...
- 一个http请求发送到后端的详细过程
我们来看当我们在浏览器输入http://www.mycompany.com:8080/mydir/index.html,幕后所发生的一切. 首先http是一个应用层的协议,在这个层的协议,只是一种通讯 ...
- 《LINUX内核设计与实现》第一、二章学习总结
第一章 Linux内核简介 (一)Unix是一个强大.健壮和稳定的操作系统,特点是: Unix很简洁,仅仅提供几个几百个系统调用并且有一个非常明确的设计目的 在Unix中,所有的东西都被当作文件对待, ...
- 《Linux内核分析》第四周:扒开系统调用的三层皮
杨舒雯 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 " 一. 用户 ...