所需jar:commons-logging-1.1.3.jar、httpclient-4.3.1.jar、httpcore-4.3.jar

package com.onlyou.microfinance.common.util;import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;/*** https封装类,支持get、post* * @author Administrator**/
public class HttpsUtil {private static CloseableHttpClient client=null;private static CloseableHttpClient createHttpsClient() {if(client!=null){return client;}try {X509TrustManager x509mgr = new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] xcs, String string) {}@Overridepublic void checkServerTrusted(X509Certificate[] xcs, String string) {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}};SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, new TrustManager[]{x509mgr}, null);SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);client=HttpClients.custom().setSSLSocketFactory(sslsf).build();return client;} catch (KeyManagementException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return HttpClients.createDefault();}public static HttpEntity doGetByHttps(String url, String host, String cacheControl, String contentType, String acceptCharset, String pragma, String accept, String acceptEncoding, String referer) throws Exception {CloseableHttpClient client = createHttpsClient();HttpHost httpHost = new HttpHost(host, 443, "https");HttpGet httpGet = new HttpGet(url);httpGet.addHeader(HttpHeaders.CACHE_CONTROL, cacheControl);httpGet.addHeader(HttpHeaders.CONTENT_TYPE, contentType);httpGet.addHeader(HttpHeaders.ACCEPT_CHARSET, acceptCharset);httpGet.addHeader(HttpHeaders.PRAGMA, pragma);httpGet.addHeader(HttpHeaders.ACCEPT, accept);httpGet.addHeader(HttpHeaders.ACCEPT_ENCODING, acceptEncoding);httpGet.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2471.0 Safari/537.36");httpGet.addHeader(HttpHeaders.REFERER, referer);HttpResponse response = client.execute(httpHost, httpGet);HttpEntity entity = response.getEntity();if (null != entity) {//String result = EntityUtils.toString(httpEntity); //byte[] data = EntityUtils.toByteArray(httpEntity); return entity;} else { return null;}}public static HttpEntity doGetByHttps(String url, String host, String contentType, String referer) throws Exception {return doGetByHttps(url, host, "no-cache", contentType, "utf-8", "no-cache", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "gzip, deflate, sdch", referer) ;}public static HttpEntity doPostByHttps(String url, String host, String cacheControl, String contentType, String acceptCharset, String pragma, String accept, String acceptEncoding, String referer, Map<String, Object> paramMap) {HttpHost httpHost = new HttpHost(host, 443, "https");HttpPost httpRequst = new HttpPost(url);httpRequst.addHeader(HttpHeaders.CACHE_CONTROL, cacheControl);httpRequst.addHeader(HttpHeaders.CONTENT_TYPE, contentType);httpRequst.addHeader(HttpHeaders.ACCEPT_CHARSET, acceptCharset);httpRequst.addHeader(HttpHeaders.PRAGMA, pragma);httpRequst.addHeader(HttpHeaders.ACCEPT, accept);httpRequst.addHeader(HttpHeaders.ACCEPT_ENCODING, acceptEncoding);httpRequst.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2471.0 Safari/537.36");httpRequst.addHeader(HttpHeaders.REFERER, referer);List<NameValuePair> params = new ArrayList<>();if (paramMap != null && !paramMap.isEmpty()) {for (String key : paramMap.keySet()) {params.add(new BasicNameValuePair(key, (String) paramMap.get(key)));}}try {httpRequst.setEntity(new UrlEncodedFormEntity(params));CloseableHttpClient client = createHttpsClient();HttpResponse httpResponse = client.execute(httpHost, httpRequst);if (httpResponse.getStatusLine().getStatusCode() == 200) {HttpEntity httpEntity = httpResponse.getEntity();//String result = EntityUtils.toString(httpEntity); //byte[] data = EntityUtils.toByteArray(httpEntity); return httpEntity;}} catch (Exception e) {e.printStackTrace();}  return null;}public static HttpEntity doPostByHttps(String url, String host, String contentType, String referer, Map<String, Object> paramMap) throws Exception {return doPostByHttps(url, host, "no-cache", contentType, "utf-8", "no-cache", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "gzip, deflate, sdch", referer, paramMap) ;}
}

调用示例:

HttpEntity loginEntity = HttpsUtil.doPostByHttps(url, "ipcrs.pbccrc.org.cn", "application/x-www-form-urlencoded", "https://*/page/login/loginreg.jsp", map);HttpEntity entity = HttpsUtil.doGetByHttps(url, host, "image/jpeg", "https://*/page/login/loginreg.jsp");

转载于:https://www.cnblogs.com/linjiqin/p/4745934.html

https封装类,支持get/post请求相关推荐

  1. Jersey框架三:Jersey对HTTPS的支持

    Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...

  2. SpringBoot2.1.5 (22)--- SpringBoot设置支持跨域请求

    SpringBoot2.1.5 (22)--- SpringBoot设置支持跨域请求 现代浏览器处于安全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求, ...

  3. 阿里云个人站点基于nginx代理搭建https协议支持

    阿里云个人站点基于nginx代理搭建https协议支持 准备工作 购买免费个人版dv证书 配置nginx 开放防火墙端口 检测是否成功 准备工作 1.个人服务器 2.安装了ssl_moudle的ngi ...

  4. 淘宝商品详情页视频接口(视频参数,sku属性参数,销量参数等页面上的数据均可以采集,支持高并发请求)

    淘宝商品详情页视频接口(视频参数,sku属性参数,销量参数等页面上的数据均可以采集,支持高并发请求)接口代码教程如下: 1.公共参数 名称 类型 必须 描述 key String 是 调用key(必须 ...

  5. 解决:ngxin做http强制跳转https,接口的POST请求变成GET

    解决:ngxin做http强制跳转https,接口的POST请求变成GET 域名配置了http强制跳转htpps后发现app发起post请求会出现405错误. 所以怀疑是http强制跳转https出现 ...

  6. 如何制作SSL证书即https服务支持

    如何制作SSL证书即https服务支持 目的 最近需要做个网站,支持https的访问,但是,是内部使用的,不需要对外网开放. 基础知识 在制作之前先了解一下OpenSSL.HTTPS的关系.见http ...

  7. 基于 cz88 纯真IP数据库开发的 IP 解析服务 - 支持 http 协议请求或 rpc 协议请求,也支持第三方包的方式引入直接使用

    cz88 基于 cz88 纯真IP数据库开发的 IP 解析服务 - 支持 http 协议请求或 rpc 协议请求,也支持第三方包的方式引入直接使用 Go 语言编写 进程内缓存结果,重复的 ip 查询响 ...

  8. 浏览器http无法调用摄像头,https才支持调用摄像头

    http协议因为安全原因,无法在普通浏览器中调用摄像头.麦克风等. https是支持的. 如果https不现实的话,可参考大神的方法设置看是否有效. https://blog.csdn.net/dyl ...

  9. 在阿里云centos7.4上配置nginx免费的https证书,支持泛解析

    在阿里云centos7.4上配置nginx免费的https证书,支持泛解析 一 原理说明: 使用acme.sh工具来生成证书,但为了方面采用的使用dns添加TXT记录验证方式,跟传统的webroot有 ...

  10. 支持百亿请求的微博广告运维技术实践

    来自:DBAplus社群 本文根据朱伟老师在[2019 Gdevops全球敏捷运维峰会-广州站]现场演讲内容整理而成. 讲师介绍 朱伟,微博广告SRE团队负责人,书籍<智能运维:从0搭建大规模分 ...

最新文章

  1. 计算机视觉大规模爆发,6大细分领域将撑起725亿元市场
  2. 在mac上命令行里面如何打开文本编辑器?
  3. Fast digital I/O for Arduino
  4. AXI4总线协议的发展历史
  5. 模态对话框和非模态对话框的消息循环分析
  6. mysql storage_mySQL__storage课堂笔记和练习
  7. Dinosaur Run - Dinosaur world Games
  8. 森林病虫防治系统 (七)
  9. 用SqlConnectionStringBuilder修改连接超时时间
  10. python数据分析推荐课程_coursera上有哪些值得学习的Python,数据分析的课程
  11. 安卓下最强的3款pdf阅读器测评
  12. 数电4_4——常见组合逻辑电路(3)数据选择器
  13. DDOS攻击与防御(一)
  14. 浏览器 本地html 图片不显示,网页不显示图片怎么解决?
  15. 数字改造有色金属产业链,发挥产业优势效能
  16. 电子元器件行业采购管理平台数字化采购,助力企业降本增效
  17. MacBook Pro 2018电池鼓包、键盘问题免费换新
  18. 曹云金回应公式相声_疑砸挂曹云金?阎鹤祥封箱大典开玩笑要退社,郭德纲回复亮了!...
  19. 2021四川高考成绩位次查询,四川高考排名对应学校-四川高考位次查询(2021年文科参考)...
  20. 张涵诚对于大数据在旅游业的应用场景分享

热门文章

  1. execute immediate 用法详解
  2. 验证OpenVino时,找不到指定模块的解决
  3. Zynga公布2020年第三季度财务业绩
  4. 强大TOP版淘客程序(带后台管理)
  5. C语言异或运算逆运算,异或的逆运算
  6. win10应用商店恢复
  7. win10上成功运行faster-rcnn.pytorch-1.0
  8. ubuntu登录mysql报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mys
  9. 曲卉:高阶增长黑客实战营
  10. linux基础及应用教程第二版课后答案,Linux基础及应用复习题(附加参考答案)