HttpClient4.3.6 实现https访问
package httptest; import java.io.IOException;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; /**
* @author yan
* @date 2016-6-9 11:03:04
* @version V1.0
* @desc
*/
public class HttpsUtil { public static final String get(final String url, final Map<String, Object> params) {
StringBuilder sb = new StringBuilder(""); if (null != params && !params.isEmpty()) {
int i = 0;
for (String key : params.keySet()) {
if (i == 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(params.get(key));
i++;
}
} CloseableHttpClient httpClient = createSSLClientDefault(); CloseableHttpResponse response = null;
HttpGet get = new HttpGet(url + sb.toString());
String result = ""; try {
response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
} return result;
} public static final String post(final String url, final Map<String, Object> params) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url); CloseableHttpResponse response = null; if (null != params && !params.isEmpty()) {
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
nvpList.add(nvp);
}
post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
} String result = ""; try {
response = httpClient.execute(post); if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
} return result;
} private static CloseableHttpClient createSSLClientDefault() { SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string){
return true;
}
}).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (KeyManagementException ex) {
Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
} return HttpClients.createDefault();
} public static void main(String[] args) {
System.out.println("Result:" + get("https://github.com/", null));
}
}
HttpClient4.3.6 实现https访问的更多相关文章
- Tomcat创建HTTPS访问,java访问https
一 https和ssL HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的 ...
- Windows下Nginx配置SSL实现Https访问(包含证书生成)
Vincent.李 Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...
- wdcp 下apache模式开启https访问,支持多站点
1.vi conf/httpd.conf 查找 #Include conf/extra/httpd-ssl.conf (删除行首的配置语句注释符号"#"保存退出) 2.vi con ...
- PHP curl https访问问题
PHP curl https访问问题,原代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /* @String url URL地址 * @Array data P ...
- 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问
参考页面: https://certbot.eff.org/#ubuntutrusty-nginx http://bbs.qcloud.com/thread-12059-1-1.html http:/ ...
- nginx使用ssl模块配置支持HTTPS访问
默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求: 做一个网站域名为 www.localhost.cn 要求通过htt ...
- CentOS搭建svn服务器支持https访问
在CentOS6.3 64位机器上配置SVN服务器,并设置只允许HTTPS连接,可以配置多个repos源,每个源都拥有自己的组和成员,用于权限控制. 安装相关软件 Apache yum install ...
- Chrome以https访问gitlab的问题:Your connection is not private
在Chrome中以https访问自己搭建的gitlab站点时经常出现下面的错误: Attackers might be trying to steal your information from xx ...
- 记一次https访问握手失败(handshake failure)
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6239518.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...
随机推荐
- 在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法
解决办法: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext. ...
- win8 64位 mysql安装 Configuration file my.ini error code -1
问题如题,解决方法: 1.由于目录中纯在中文,所以导致代码错误.故而把目录设置成全英文的. 2.下载64位mysql安装,安装32位mysql也会出现此问题.
- MySQL字符串函数
字符串大写和小写转换 MySQL 字符串大写和小写转化函数有两对: lower(), uppper() 和 lcase(), ucase() mysql> select lower('DDD') ...
- IE8下提示'console'没有定义错误
在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...
- 应用层协议实现系列(一)——HTTPserver之仿nginx多进程和多路IO的实现
近期在尝试自己写一个Httpserver,在粗略研究了nginx的代码之后,决定仿照nginx中的部分设计自己实现一个高并发的HTTPserver,在这里分享给大家. 眼下使用的较多的Httpserv ...
- LeetCode201 Bitwise AND of Numbers Range Java 题解
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
- 移植opencv库到zedboard(制作运行库镜像) 分类: OpenCV ZedBoard ubuntu shell Eye_Detection 2014-11-08 18:48 172人阅读 评论(0) 收藏
主要参考rainysky的博客 http://ledage.eefocus.com/sj229335457/blog/13-06/295352_ad954.html opencv的话只需要将lib这个 ...
- systrace跟踪 Android性能优化
http://blog.csdn.net/oujunli/article/details/8138172 http://blog.csdn.net/oujunli/article/details/50 ...
- exit与return区别
1. exit用于结束正在运行的整个程序,它将参数返回给OS,把控制权交给操作系统:而return 是退出当前函数,返回函数值,把控制权交给调用函数.2. exit是系统调用级别,它表示一个进程的结束 ...
- INERT DELEYED、INSERT IGNORE replace into和insert区别
insert into表示插入数据,数据库会检查主键,如果出现重复会报错:replace into表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据 ...