所需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() {
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) {
} @Override
public void checkServerTrusted(X509Certificate[] xcs, String string) {
} @Override
public 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封装类,支持get/post请求的更多相关文章

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

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

  2. django 实现全局支持跨域请求

    Django 实现允许跨域请求 1.安装django-cors-headers pip install django-cors-headers 2.配置settings.py文件 INSTALLED_ ...

  3. chrome 等浏览器不支持本地ajax请求的问题

    chrome 等浏览器不支持本地ajax请求的问题 XMLHttpRequest cannot load file:///D:/WWW/angularlx/ui-router-test/templat ...

  4. 咏南中间件增加HTTPS.SYS支持

    咏南中间件增加HTTPS.SYS支持 老客户可免费升级. HTTPS.SYS可以开发强大而稳定的REST SERVER. 微软在Windows Vista (server 2008) 以后使用http ...

  5. 如何在iOS上实现对HTTPS的支持(转)

    原文地址:http://blog.5ibc.net/p/101504.html 首先,需要明确你使用HTTP/HTTPS的用途,因为OSX和iOS平台提供了多种API,来支持不同的用途,官方文档< ...

  6. java5增加对https的支持

    jdk1.5不支持http协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 利用httpclient,进行封装,从而实现对h ...

  7. SpringBoot设置支持跨域请求

    跨域:现代浏览器出全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求,默认情况下是被禁止的,ip(域名)不同.或者端口不同.协议不同(比如http.htt ...

  8. AFNETWorking 不支持中文URL请求

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p ...

  9. Chrome不支持本地Ajax请求解决?

    今天用JQuery操作Ajax时,使用load方法加载html块 结果提示: XMLHttpRequest cannot load file~~~~~~~Origin 'null' is theref ...

随机推荐

  1. python 获取文件夹大小

    __author__ = 'bruce' import os from os.path import join,getsize def getdirsize(dir): size=0l for (ro ...

  2. addClass() 和 toggleClass()

    addClass()是在原有的类基础上增加类属性,仍然保留原有的类的样式.语法格式为:addClass(class0 class1 ...) ,例如: $("p").addClas ...

  3. The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决

    问题 The certificate used to sign "AppName" has either expired or has been revoked. An updat ...

  4. machine learning----->谷歌Cloud Machine Learning平台

    1.谷歌Cloud Machine Learning平台简介: 机器学习的三要素是数据源.计算资源和模型.谷歌在这三个方面都有强大的支撑:谷歌不仅有种类丰富且数量庞大的数据资源,而且有强大的计算机群提 ...

  5. Java获取服务器网址

    StringBuffer url1 = request.getRequestURL(); String tempContextUrl1 = url1.delete(url1.length() - re ...

  6. freemarker 数字格式化函数

    ${num?string('0.00')} 如果小数点后不足两位,用 0 代替 ${num?string('#.##')} 如果小数点后多余两位,就只保留两位,否则输出实际值 输出为:1239765. ...

  7. 从php脚本到浏览器,编码方式浅析

    今天简单看了一下php,服务器,浏览器如何设定编码 1.php 在php配置文件php.ini中,可以设置字符编码 ; PHP's default character set is set to em ...

  8. [学习笔记] 七步从AngularJS菜鸟到专家(7):Routing [转]

    这是"AngularJS – 七步从菜鸟到专家"系列的第七篇. 在第一篇,我们展示了如何开始搭建一个AngularaJS应用.在第四.五篇我们讨论了Angular内建的directives,上一篇了解 ...

  9. freeCodeCamp:Repeat a string repeat a string

    重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. /*思路 fo循环将字符串重复num次并组成数组 将数组组成新的字符串并返回 */ function repeat(str, ...

  10. 面向对象、类与对象、成员与局部变量、封装、private、构造函数、this、static、extends、super、final、abstract、interface、多态、内部类、异常【5】

    若有不正之处,请多多谅解并欢迎批评指正,不甚感激. 请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing ...