HTTPClient 发送HTTPS请求
来源:互联网 发布:seo可以自学吗 编辑:程序博客网 时间:2024/05/16 18:38
HTTPClient 发送HTTP请求就不多说了, 现在给出发送HTTPS请求, 主要思路是忽略证书验证.
/** * * @param url * @param contextType "image/jpeg","application/Json" * @return */public static byte[] sendHttpsGetUrl(HttpClient httpClient1 ,String url,String contextType) {// 响应内容byte[] bs = null;// 创建默认的httpClient实例//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient =httpClient1;// 创建TrustManagerX509TrustManager xtm = new X509TrustManager() {public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException {}public void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[] {};}};try {SSLContext ctx = SSLContext.getInstance("SSL");// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用ctx.init(null, new TrustManager[] { xtm }, null);SSLSocketFactory sf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);Scheme sch = new Scheme("https", 443, sf);httpClient.getConnectionManager().getSchemeRegistry().register(sch);// 创建HttpPostHttpGet httpPost = new HttpGet(url); httpPost.setHeader("content-type", contextType);// 执行POST请求HttpResponse response = httpClient.execute(httpPost); // 获取响应实体HttpEntity entity = response.getEntity(); bs = IOUtils.toByteArray(entity.getContent()); if (null != entity) {EntityUtils.consume(entity); // Consume response content}return bs;} catch (Exception e) {e.printStackTrace();} finally {// 关闭连接,释放资源//httpClient.getConnectionManager().shutdown(); }return bs;}
下面给出Maven required:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.3</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>
0 0
- HTTPClient 发送HTTPS请求
- httpClient发送https请求
- HttpClient 发送 HTTP、HTTPS 请求
- httpClient发送https请求代码
- HttpClient 之 发送Https请求
- 使用HttpClient发送https请求GET方式
- HttpClient向HTTPS地址发送POST请求
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- 使用HttpClient向HTTPS地址发送POST请求4.1.2
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- HttpClient学习笔记 --发送Http和Https请求
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- 使用Apache提供的HttpClient发送https请求
- httpclient发送https的get请求忽略证书
- HttpClient 4.3 发送HTTPS
- HttpClient请求Https协议
- 暴雪总裁:告诉你魔兽世界这么牛的原因!(转载)
- Android点9图原理以及读取点9图的源代码
- COCOS2DX-3.0中如何调用LUA
- Java基本数据类型
- Linux后台开发应该具备技能
- HTTPClient 发送HTTPS请求
- C# 多线程
- Android - 缺少NDT选项和C/C++ Build选项 问题
- 黑马程序员——java概述
- qt根据文件名取得图标
- 十大编程算法助程序员走上高手之路
- OpenERP国际化
- Matlab读取和保存TXT文件数据
- 独立看门狗