httpclient4.3访问https
2015-11-19 10:14
573 查看
1、创建一个访问https的工具类
2测试
package org.aaa.tool; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import javax.net.ssl.SSLContext; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import com.alibaba.fastjson.JSONObject; public class HttpClientUtil { private static Logger logger = Logger.getLogger(HttpClientUtil.class); private static final int TIME_OUT = 120000; public static CloseableHttpClient createSSLClientDefault(){ try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { //信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return HttpClients.createDefault(); } }
2测试
CloseableHttpClient httpClient = HttpClientUtil.createSSLClientDefault(); HttpGet get = new HttpGet(); get.setURI(new URI("你的https://地址")); httpClient.execute(get)
相关文章推荐
- Android OkHttp完全解析 是时候来了解OkHttp了
- Linux进程调度的目标 http://www.cnblogs.com/zhaoyl/archive/2012/09/04/2671156.html
- [转载]复杂网络常见的数据和程序代码网站
- [转载]复杂网络资源
- The MagicAjax HttpModule is not included in web.config. Add [<httpModules><add name="MagicAjax" type
- 写了一个下载网络图片的功能,可以用
- 深度学习基础1(神经网络)
- TCPL 行计数
- 动态监听网络状态
- 【读书笔记】iOS-苹果的网络
- 【读书笔记】iOS-苹果的网络
- HTTP403(禁止访问)---WampServer 127.0.0.1 禁止访问
- Feng是LSCUBE维护的开源流媒体服务器,兼容IETF标准,实现了RTSP、RTP/RTCP
- Fiddler工具安装介绍
- Ubuntu下libpcap安装步骤
- 网络下载图片大全
- GPRS流量计算方法(TCP/IP)
- Java网络学习笔记3:设置Socket选项(补充)
- CentOS网络配置
- HTTP协议下Android web应用GET和POST请求方法的请求、响应、传参