一.https忽略证书

/**
* 用于进行Https请求的HttpClient
*
* @author joey
*
*/
public class SSLClient {
public static CloseableHttpClient createSSLClientDefault(){
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
} }

二.post与get请求

/**
* 利用HttpClient的工具类
*
* @author Joey
*
*/
public class HttpClientUtil { private static String charSet = "UTF-8";
private static CloseableHttpClient httpClient = null;
private static CloseableHttpResponse response = null; /**
* https的post请求
* @param url
* @param jsonstr
* @param charset
* @return
*/
public static String doHttpsPost(String url, String jsonStr) {
try {
httpClient = SSLClient.createSSLClientDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json"); StringEntity se = new StringEntity(jsonStr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se); response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
return EntityUtils.toString(resEntity, charSet);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的post请求(用于key-value格式的参数)
* @param url
* @param param
* @return
*/
public static String doHttpPost(String url,Map<String,String> param){
try {
//请求发起客户端
httpClient = HttpClients.createDefault();
//参数集合
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
//遍历参数并添加到集合
for(Map.Entry<String, String> entry:param.entrySet()){
postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
} //通过post方式访问
HttpPost post = new HttpPost(url);
HttpEntity paramEntity = new UrlEncodedFormEntity(postParams,charSet);
post.setEntity(paramEntity);
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity valueEntity = response.getEntity();
String content = EntityUtils.toString(valueEntity);
//jsonObject = JSONObject.fromObject(content);
return content;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的post请求(用于请求json格式的参数)
* @param url
* @param params
* @return
*/
public static String doHttpPost(String url, String jsonStr) {
try {
httpClient = HttpClients.createDefault(); // 创建httpPost
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json"); StringEntity entity = new StringEntity(jsonStr, charSet);
entity.setContentType("text/json");
entity.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(entity);
//发送post请求
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的Get请求
* @param url
* @param param
* @return
*/
public static String doHttpGet(String url,Map<String,String> param) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null; try {
httpclient = HttpClients.createDefault();
if(param != null && !param.isEmpty()) {
//参数集合
List<NameValuePair> getParams = new ArrayList<NameValuePair>();
for(Map.Entry<String, String> entry:param.entrySet()){
getParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
url +="?"+EntityUtils.toString(new UrlEncodedFormEntity(getParams), "UTF-8");
}
//发送gey请求
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(httpclient != null){
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} }

封装HttpClient进行http请求与https请求的更多相关文章

  1. httpclient绕过证书验证进行HTTPS请求

    http请求是我们常用的一种web应用的应用层协议,但是由于它的不安全性,现在正在逐渐向https协议过渡.https协议是在http的基础上进行了隧道加密,加密方式有SSL和TLS两种.当serve ...

  2. httpclient 3.1跳过https请求SSL的验证

    一.因为在使用https发送请求的时候会涉及,验证方式.但是这种方式在使用的时候很不方便.特别是在请求外部接口的时候,所以这我写了一个跳过验证的方式.(供参考) 二.加入包,这里用的是commons- ...

  3. 支持https请求以及https请求的抓包

    iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...

  4. Java 使用代理发送Http请求 (将Http请求代理Https请求)

    package com.test.porxy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  5. HttpClient 之 发送Https请求

    HttpClient包是一个优秀的Http请求的开源jar. 本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求. 但是由于项目的需要快速的实现,以下代码还可能会有点过 ...

  6. [PHP自动化-进阶]003.CURL处理Https请求访问

    引言:继前文<模拟登录并采集数据>,<模拟登录带有验证码的网站>,大家对CURL基本上已经有了认识,这一讲简单的说一下请求Https. 在很多的站点,如TalkingData, ...

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

  8. AFNetWorking3.0使用 自签名证书的https请求

    前几日,项目组出于安全角度的考虑,要求项目中的请求使用https请求,因为是企业内部使用的app,因此使用了自签名的证书,而自签名的证书是不受信任的,所以我们就需要自己来做证书的验证,包括服务器验证客 ...

  9. Volley框架支持HTTPS请求。

    第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...

随机推荐

  1. socketserver模块初识

    python提供了两个级别访问的网络服务: 低级的网络服务支持基本的socket,它提供了标准的BSD sockets API,可以访问底层操作系统socket接口的全部方法 高级别的网络服务模块so ...

  2. Spring学习总结(15)——Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  3. 关系数据库标准语言SQL

    篇幅过长,恐惧者慎入!!!基础知识,大神请绕道!!! 本节要点: l  SQL概述 l  学生-课程关系 l  数据定义 基本表的定义.删除与修改 索引的建立与删除 l  查询 单表查询 连接查询 嵌 ...

  4. Codeforces Round #464 (Div. 2)

    A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...

  5. linux下添加自定义脚本到开机自启动的方法

    原文链接:http://www.jb51.net/LINUXjishu/183462.html 我的机器有个coreseek服务,但是没加到开启启动中去,导致机房一旦重启了机器,我的服务便不能使用了. ...

  6. HBase读取代码

    HBase读取代码 需要的jar包: activation-1.1.jar aopalliance-1.0.jar apacheds-i18n-2.0.0-M15.jar apacheds-kerbe ...

  7. nyoj 119 士兵杀敌(三) 【线段树】【单点更新】

    题意:. .. 策略如题. 思路:我们先如果仅仅求某一区间的最大值.我们仅仅须要利用线段树的模板.仅仅须要初始化和询问的时候小小的改动一下.改成祖先结点储存的不再是子节点的和而是两个子节点之间的最大值 ...

  8. 基于lucene的案例开发:纵横小说分布式採集

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/46812645 http://www.llwjy.com/blogdetail/9 ...

  9. Android——隐藏输入法的小技巧

    今天偶然在百度地图提供的DEMO里看到这样一段代码.认为确实是个小技巧,就写下来分享一下. 针对的问题: 我们在开发android界面的时候,常常使用EditText控件.然后每次进入这个页面的时候, ...

  10. java 顺序 读写 Properties 配置文件 支持中文 不乱码

    java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...