Https请求忽略证书验证最新实现
来源:互联网 发布:docker mac 编辑:程序博客网 时间:2024/05/04 02:57
最近工作中需要和一个第三方公司进行https交互请求,但是对方的证书有一些问题,所以在发送请求的时候需要忽略证书验证。百度之后,发现已经有很多这方面的介绍,不过在使用其代码的时候总会有一些类不推荐使用了。下面是参考网上的常见方面并结合最新的官方API实现的一个最新方法(使用的主要jar包括httpclient-4.5.1.jar和httpcore-4.4.3.jar)。
public static List doPostByClient(String url, Map<?, ?> postData, Map<?, ?> header,String encoding, long connectionTimeout, long soTimeout,boolean isNoSSL)throws Exception {CloseableHttpClient client = HttpClients.createDefault();if(isNoSSL){client = (CloseableHttpClient)wrapClient(client);}HttpPost httpPost = new HttpPost(url);RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout((int) soTimeout).setConnectTimeout((int) connectionTimeout).build();//设置请求和传输超时时httpPost.setConfig(requestConfig);// 头部请求信息if (header != null) {Set<?> entrySet = header.entrySet();for (Iterator<?> itor = entrySet.iterator(); itor.hasNext();) {Map.Entry entry = (Map.Entry) itor.next();httpPost.addHeader(entry.getKey().toString(), entry.getValue().toString());}}List<NameValuePair> parameters = new ArrayList<NameValuePair>();if (postData != null) {Set<?> entrySet = postData.entrySet();for (Iterator<?> itor = entrySet.iterator(); itor.hasNext();) {Map.Entry entry = (Map.Entry) itor.next();parameters.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue() + ""));}// 创建UrlEncodedFormEntity对象UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters, encoding);httpPost.setEntity(formEntiry);}// 执行请求HttpResponse response = client.execute(httpPost);response.getStatusLine();StatusLine status = response.getStatusLine();int sc = status.getStatusCode();if (sc != 200) {return null;}HttpEntity entity = response.getEntity();if (entity != null) {InputStream is = entity.getContent();BufferedReader bin = new BufferedReader(new InputStreamReader(is,"utf-8"), 1024 * 1024);List result = new ArrayList();while (true) {String line = bin.readLine();if (line == null) {break;} else {result.add(line);}}return (result);} else {return null;}}/** * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”异常 * 不用导入SSL证书 * @param base * @return */public static HttpClient wrapClient(HttpClient base) {try {SSLContext ctx = SSLContext.getInstance("TLS");X509TrustManager tm = new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {}public void checkServerTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {}};ctx.init(null, new TrustManager[] { tm }, null);SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx,NoopHostnameVerifier.INSTANCE);CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(ssf).build();return httpclient;} catch (Exception ex) {ex.printStackTrace();return HttpClients.createDefault();}}说明:其中wrapClient方法就是创建一个不进行正式验证的请求客户端对象。
参考文档:
1.http://www.th7.cn/Program/java/201402/173791.shtml Https请求基本过程介绍;
2.http://blog.csdn.net/kobejayandy/article/details/44284765 老版本Https请求的常见实现;
3.http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html Https请求使用类的最新官方API说明。
0 0
- Https请求忽略证书验证最新实现
- Https请求忽略证书验证最新实现
- iOS https请求 忽略证书验证
- httpclient忽略证书验证允许https请求
- AFNetworking 3.0 https请求忽略证书验证
- HttpPost忽略证书请求https
- OKHttp HTTPS 请求支持所有Https服务器 证书验证忽略客户端证书,(3)
- 【Android Trick 2】HTTPS请求忽略证书
- httpclient https忽略证书直接请求
- okhttp使用https忽略证书验证
- okhttp使用https忽略证书验证
- CloseableHttpAsyncClient忽略https的证书验证
- 使用自定义证书并忽略验证的HTTPS连接Post请求方式的封装
- OKHttp HTTPS 请求证书验证 PEM证书
- 验证 HTTPS 请求的证书(五)
- 验证 HTTPS 请求的证书(五)
- 验证 HTTPS 请求的证书(五)
- iOS手机客户端https请求忽略证书认证
- Android - Event Handling
- hdu 5137(2014广州—最短路)
- 微软Windows 10硬件新品发布会
- 软件测试知识点总结(五)——动态测试
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- Https请求忽略证书验证最新实现
- 卷积算子计算方法(卷积运算)
- QWS Server
- 实现一个label中的文本有不同的文字颜色
- linux常用命令(65):killall命令
- Java基本类型与引用类型
- Redis服务器搭建/配置/及Jedis客户端的使用方法
- AF网络封装
- 51nod 1208 && POJ 2482:Stars in Your Window