所需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. 在Hadoop-2.2.0集群上安装 Hive-0.13.1 with MySQL

    fesh个人实践,欢迎经验交流!本文Blog地址:http://www.cnblogs.com/fesh/p/3872872.html 软件环境 操作系统:Ubuntu14.04 JDK版本:jdk1 ...

  2. [DB]MariaDB 与 MySql 数据库

    目前 MariaDB 已经出来几年了,本文编辑时的官网最新稳定版本是 10.1.14 链接:https://downloads.mariadb.org/ 但百度没有下载,搜狗给的下载版本是 5.5.2 ...

  3. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  4. c语言检测文件是否存在int __cdecl access(const char *, int);

    最近写代码,遇到很多地方需要判断文件是否存在的.网上的方法也是千奇百怪,“百家争鸣”. fopen方式打开的比较多见,也有其他各种方式判断文件是否存在的,由于其他方法与本文无关,所以不打算提及. 笔者 ...

  5. 手动开启/关闭Oracle数据库

    @echo off@echo 启动/关闭数据库set /p flag=您是否要启动数据库?(是按Y启动,否按N关闭) goto answer%flag% goto end :answery echo ...

  6. 使用 IntraWeb (43) - 测试读取 SqLite (二)

    一般情况下, 数据源相关控件应该有数据模块中统一管理, 这也方便其他窗体调用; UserSessionUnit 就是一个现成的数据模块. 现在把数据源相关控件放在 UserSessionUnit 的窗 ...

  7. VS2013使用rtklib中需要注意的一些问题(编译)

    最近因为项目需要需要对rtcm数据进行解码,rtklib提供了很多底层的函数,准备直接输出标准DLL的方式供C#调用.下面把项目中引用rtklib源码需要注意的地方记录下. 1. 首先在vs2013中 ...

  8. XE3随笔10:TSuperType

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. js获取文件大小

    var file = urlBox.doc.activeElement.files[0]||urlBox.files[0] ; if (file) { var fileSize = 0; if (fi ...

  10. linux下挂载新硬盘

    挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 1.创建新硬盘分区 用fdisk  + 路径 进行分区 进入磁盘,对磁盘进行分区 #fdisk /dev/sdb Command (m for h ...