https封装类,支持get/post请求
所需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请求的更多相关文章
- Jersey框架三:Jersey对HTTPS的支持
Jersey系列文章: Jersey框架一:Jersey RESTful WebService框架简介 Jersey框架二:Jersey对JSON的支持 Jersey框架三:Jersey对HTTPS的 ...
- django 实现全局支持跨域请求
Django 实现允许跨域请求 1.安装django-cors-headers pip install django-cors-headers 2.配置settings.py文件 INSTALLED_ ...
- chrome 等浏览器不支持本地ajax请求的问题
chrome 等浏览器不支持本地ajax请求的问题 XMLHttpRequest cannot load file:///D:/WWW/angularlx/ui-router-test/templat ...
- 咏南中间件增加HTTPS.SYS支持
咏南中间件增加HTTPS.SYS支持 老客户可免费升级. HTTPS.SYS可以开发强大而稳定的REST SERVER. 微软在Windows Vista (server 2008) 以后使用http ...
- 如何在iOS上实现对HTTPS的支持(转)
原文地址:http://blog.5ibc.net/p/101504.html 首先,需要明确你使用HTTP/HTTPS的用途,因为OSX和iOS平台提供了多种API,来支持不同的用途,官方文档< ...
- java5增加对https的支持
jdk1.5不支持http协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 利用httpclient,进行封装,从而实现对h ...
- SpringBoot设置支持跨域请求
跨域:现代浏览器出全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求,默认情况下是被禁止的,ip(域名)不同.或者端口不同.协议不同(比如http.htt ...
- 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 ...
- Chrome不支持本地Ajax请求解决?
今天用JQuery操作Ajax时,使用load方法加载html块 结果提示: XMLHttpRequest cannot load file~~~~~~~Origin 'null' is theref ...
随机推荐
- WebForm ASP开发方式、 IIS服务器以及WebForm开发基础
网页端 B/S两种:(ASP.NET --网站应用开发技术) WebForm: 微软最先出现的网站开发技术 MVC: 后来开发,为了让java.PHP转过来的开发人员更容易接受使用 客户端 C/ ...
- HTML插入Flash的全兼容完美解决方案-SWFObject
Flash 嵌入的问题朋友们可能认为很简单,但是具体到一些问题上来讲,就不是那么简单了,比如:IE浏览器对FLASH的拦截虚框怎么去掉?在FF.OP.NS等其它浏览器是否兼容?代码是否符合W3C标准? ...
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
Android中的ImageView只能显示矩形的图片,为了用户体验更多,Android实现圆角矩形,圆形或者椭圆等图形,一般通过自定义ImageView来实现,首先获取到图片的Bitmap,然后通过 ...
- .NET实现高效过滤敏感查找树算法(分词算法):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Linux命令(1)- grep
1.grep 功能:查找文件里符合条件的字符串. 语法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数& ...
- OA项目之导出
要导出页的前台: <asp:Button runat="server" ID="btnExport" Text="导出" CssCla ...
- Spring 4 官方文档学习(十一)Web MVC 框架之HTTP caching support
一个良好的HTTP缓存策略可以显著地增进web应用的性能和其客户端的体验.主要使用"Cache-Control" HTTP response header来完成,配合conditi ...
- bootstrap的小图标
bootstrapt的小图标 关于bootstrap的<i>小图标,需要几个要素.<i class="icon-search"></i>形式第 ...
- hibernate多表查询,结果封装在自己定义的一个实体类当中(在自己定义的类中增加构造函数)
hibernate的hql查询直接返回java对象时出现问题3 向大家请教一个问题,现在有三张表,表之间没有关联,我需要将三张表里面的所有东西查询出来存储到一个新的对象中,该如何实现,使用hibern ...
- sql常用语法
--在表中添加字段if col_length('JX_DomesticStudy','XL') is null begin alter table JX_DomesticStudy add PXlev ...