所需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. union all 里面的order by

    例1: SELECT 1 order2 FROM dual union all SELECT 3 order2 FROM dual union all SELECT 2 order1 FROM dua ...

  2. 2015年可用的TRACKER服务器大全

    udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...

  3. 学习笔记008之Task

    栈 为后进先出 如何实现一个弹出窗体.

  4. machine learning----->学习成绩

    斯坦福大学机器学习公开课学习成绩:

  5. 解决maven依赖传递中的版本冲突问题

    通常情况下,我们都比较喜欢使用maven进行项目管理,要加个依赖包也非常简单,不需要到处去下载jar包,当然除了maven之外,也还有一些非常不错的工具.在使用maven进行项目依赖管理的时候,有时候 ...

  6. C#版SQLHelper.cs类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  7. 开源PLM软件Aras详解三 服务端简易开发

    废话少说,直接进入主题, 以CAD为例: 先找到CAD对象类:具体操作见下图 双击打开,找到服务端事件:见下图 点击新建对象,点击添加,新建Method 编写Method,语言分为前端和后端,前端支持 ...

  8. 安全关闭多Activity的Application

    1.发送广播给每一个打开的Activity. 2.采用startActivityForResult()方法递归关闭. 3.使用EventBus框架的监听者模式,关闭时触发监听事件.

  9. 获取datagrid选择行

    var rows = $('#dg').datagrid('getChecked');     var ids = '';  for (var i = 0; i < rows.length; i ...

  10. js完美转换阿拉伯数字为数字大写(原创)

    啥都不说,直接上代码: //阿拉伯数字转换为简写汉字 function Arabia_To_SimplifiedChinese(Num) { for (i = Num.length - 1; i &g ...