博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Https发送接口的方法
阅读量:5013 次
发布时间:2019-06-12

本文共 3944 字,大约阅读时间需要 13 分钟。

之前业务调接口都是HTTP的URL

这次碰到接口地址是HTTPS

以前用的HTTPClient就不能使用了

会报一个证书错误

找了个绕开证书的方法 可以调通了

记录一下

1 //https必要复写类,通过这个类绕开证书 2 public class SSLSocketFactoryEx extends SSLSocketFactory { 3  4     SSLContext sslContext = SSLContext.getInstance("TLS"); 5  6     public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, 7             UnrecoverableKeyException { 8         super(truststore); 9 10         TrustManager tm = new X509TrustManager() {11 12             public java.security.cert.X509Certificate[] getAcceptedIssuers() {13                 return null;14             }15 16             public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {17                 // TODO Auto-generated method stub18                 19             }20 21             public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {22                 // TODO Auto-generated method stub23                 24             }25 26             27         };28 29         sslContext.init(null, new TrustManager[] { tm }, null);30     }31 32     @Override33     public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {34         return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);35     }36 37     @Override38     public Socket createSocket() throws IOException {39         return sslContext.getSocketFactory().createSocket();40     }41 }
1 //获取HttpClient 2 public static DefaultHttpClient getNewHttpsClient() { 3             try { 4                 KeyStore trustStore = KeyStore.getInstance(KeyStore 5                         .getDefaultType()); 6                 trustStore.load(null, null); 7  8                 SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); 9                 sf10                         .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);11 12                 HttpParams params = new BasicHttpParams();13                 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);14                 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);15                 HttpConnectionParams.setConnectionTimeout(params, 50000);16                 HttpConnectionParams.setSoTimeout(params, 50000);17                 SchemeRegistry registry = new SchemeRegistry();18                 registry.register(new Scheme("http", PlainSocketFactory19                         .getSocketFactory(), 80));20                 registry.register(new Scheme("https", sf, 443));21 22                 ClientConnectionManager ccm = new ThreadSafeClientConnManager(23                         params, registry);24 25                 return new DefaultHttpClient(ccm, params);26             } catch (Exception e) {27                 return new DefaultHttpClient();28             }29         }
1        //调用接口  2             HttpClient httpclient = HttpClientUtil.getNewHttpsClient(); 3             HttpPost requestHttps = new HttpPost("https://192.168.1.1:8080/test"); 4             StringEntity param = new StringEntity(setJson.toString(), "UTF-8"); 5             requestHttps.addHeader("content-type", "application/json"); 6             requestHttps.setEntity(param); 7             HttpResponse responseHttps = httpclient.execute(requestHttps); 8             InputStream is = responseHttps.getEntity().getContent(); 9             BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8"));10             StringBuffer sb = new StringBuffer();11             String data = null;12             while ((data = in.readLine()) != null) {13                 sb.append(data);14             }15             System.out.println(sb.toString() + "---**");16             if (in != null)17                 in.close();

 

posted on
2018-04-18 15:46 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mide0131/p/8875363.html

你可能感兴趣的文章
近期思考(2019.07.20)
查看>>
Apache2.4使用require指令进行访问控制
查看>>
冗余关系_并查集
查看>>
做最好的自己(Be Your Personal Best)
查看>>
如何搭建github+hexo博客-转
查看>>
HW2.2
查看>>
将Windows Server 2016 打造成工作站(20161030更新)
查看>>
5大主浏览器css3和html5兼容性大比拼
查看>>
hdu-5894 hannnnah_j’s Biological Test(组合数学)
查看>>
scss常规用法
查看>>
css定位position属性深究
查看>>
android中不同版本兼容包的区别
查看>>
Static 与 new 的问题【待解决】
查看>>
xml
查看>>
在 mvc4 WebApi 中 json 的 跨域访问
查看>>
敏捷开发文章读后感
查看>>
xposed获取context 的方法
查看>>
html5 canvas 图像处理
查看>>
He who hesitates is Lost
查看>>
php中引用&的真正理解-变量引用、函数引用、对象引用
查看>>