所需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. ngx.lua中遇到的小问题

    作者: 胡 志伟 分类: ngx_lua, 开发代码 发布时间: 2013-09-26 08:40 ė 6评论关闭 在使用ngx.redirect 到一个新的地址时,错误日志提示: lua entry ...

  2. 先进先出集合queue

    先进先出集合queue Enqueue添加到集合最后 Dequeue移除集合第一个对象并返回

  3. Java NIO教程 Channel

    Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...

  4. C# XML读写实例

    一.使用System.Xml 实例:完成如下格式配置文件的读写操作: <?xml version="1.0" encoding="UTF-8"?> ...

  5. Factory Method(工厂方法)-对象创建型模式

    1.意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 2.动机 框架使用抽象类定义和维护对象之间的关系.这些对象的创建通常也由框架 ...

  6. AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4

    1.切换工作目录 git checkout step- #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-mo ...

  7. 去掉UItableview headerview黏性(sticky)

    // 去掉UItableview headerview黏性(sticky) - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFlo ...

  8. chm手册显示已取消到该网页的导航

    解决:在chm右键查看有没有解除锁定选项.1.右键单击chm文件,选择属性:2.在最下面点击“解除锁定”并确定后,再次打开chm,就正常了

  9. kenrnel 驱动中常用的宏

    http://blog.csdn.net/uruita/article/details/7263290 1. MODULE_DEVICE_TABLE (usb, skel_table);该宏生成一个名 ...

  10. HDU2243_考研路茫茫――单词情结

    给出一些词根,问你有多少种长度为L的串包含至少一个词根. 去年就在敲这个题了,今年才敲出来,还是内牛满面之中... 要求包含至少一个的情况,需要求出所有的情况,减去一个都没有的情况就可以了. 对于给出 ...