赞
踩
1.第一种就是报错咱绕开验证这个环节
先创建一个类 MyX509TrustManager
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
-
- import javax.net.ssl.X509TrustManager;
-
- public class MyX509TrustManager implements X509TrustManager{
-
- @Override
- public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- // TODO Auto-generated method stub
- return null;
- }
-
- }
然后发送请求HttpURLConnection 的方式
- SSLContext sslcontext = SSLContext.getInstance("SSL","SunJSSE");
- sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());
- URL url = new URL(urlPath);
- HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
- public boolean verify(String s, SSLSession sslsession) {
- System.out.println("WARNING: Hostname is not matched for cert.");
- return true;
- }
- };
- HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
- HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
- //这边是HttpURLConnection
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
如果一头雾水传送门
2.方法2就是正面刚,就是要搞证书 https接口测试:java导入cer证书步骤
有个注意点就是执行
keytool -import -alias cacerts -keystore cacerts -file C:\Users\Administrator\Desktop\onecc.cer -trustcacerts
这个加红的是你的cer 名字。
并且执行这个语句的时候必须在你的
D:\jdk1.7.0_80\jre\lib\security 这个下面执行
系统是windows 恰巧你的环境变量不在c 盘
这样进入你对应的盘 不要cd 坑人!
最后补充一下查询你的证书有没有安装上的代码
keytool -list -keystore cacerts | findstr /s zrzf
有错误欢迎指正。
记得按时吃饭
by 张小白
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。