package io.renren.modules.jqr.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.springframework.util.StringUtils; /**
 * http、https 请求工具类
 * 
 * @author jiaxiaoxian
 * 
 */
public class HttpUtils { private static final String DEFAULT_CHARSET = "UTF-8"; private static final String _GET = "GET"; // GET
private static final String _POST = "POST";// POST
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000; /**
 * 初始化http请求参数
 * 
 * @param url
 * @param method
 * @param headers
 * @return
 * @throws Exception
 */
private static HttpURLConnection initHttp(String url, String method,
Map<String, String> headers) throws Exception {
URL _url = new URL(url);
HttpURLConnection http = (HttpURLConnection) _url.openConnection();
// 连接超时
http.setConnectTimeout(DEF_CONN_TIMEOUT);
// 读取超时 --服务器响应比较慢,增大时间
http.setReadTimeout(DEF_READ_TIMEOUT);
http.setUseCaches(false);
http.setRequestMethod(method);
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
if (null != headers && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
http.setRequestProperty(entry.getKey(), entry.getValue());
}
}
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
return http;
} /**
 * 初始化http请求参数
 * 
 * @param url
 * @param method
 * @return
 * @throws Exception
 */
private static HttpsURLConnection initHttps(String url, String method,
Map<String, String> headers) throws Exception {
TrustManager[] tm = { new MyX509TrustManager() };
System.setProperty("https.protocols", "TLSv1");
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL _url = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) _url.openConnection();
// 设置域名校验
http.setHostnameVerifier(new HttpUtils().new TrustAnyHostnameVerifier());
// 连接超时
http.setConnectTimeout(DEF_CONN_TIMEOUT);
// 读取超时 --服务器响应比较慢,增大时间
http.setReadTimeout(DEF_READ_TIMEOUT);
http.setUseCaches(false);
http.setRequestMethod(method);
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
if (null != headers && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
http.setRequestProperty(entry.getKey(), entry.getValue());
}
}
http.setSSLSocketFactory(ssf);
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
return http;
} /**
 * 
 * @description 功能描述: get 请求
 * @return 返回类型:
 * @throws Exception
 */
public static String get(String url, Map<String, String> params,
Map<String, String> headers) throws Exception {
HttpURLConnection http = null;
if (isHttps(url)) {
http = initHttps(initParams(url, params), _GET, headers);
} else {
http = initHttp(initParams(url, params), _GET, headers);
}
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in,
DEFAULT_CHARSET));
String valueString = null;
StringBuffer bufferRes = new StringBuffer();
// 优化,不能光使用!=null做判断。有为""的情况,防止多次空循环
while (!StringUtils.isEmpty(valueString = read.readLine())) {
bufferRes.append(valueString);
}
in.close();
if (http != null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
} public static String get(String url) throws Exception {
return get(url, null);
} public static String get(String url, Map<String, String> params)
throws Exception {
return get(url, params, null);
} public static String post(String url, String params) throws Exception {
HttpURLConnection http = null;
if (isHttps(url)) {
http = initHttps(url, _POST, null);
} else {
http = initHttp(url, _POST, null);
}
OutputStream out = http.getOutputStream();
out.write(params.getBytes(DEFAULT_CHARSET));
out.flush();
out.close(); InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in,
DEFAULT_CHARSET));
String valueString = null;
StringBuffer bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null) {
bufferRes.append(valueString);
}
in.close();
if (http != null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
} public static String post(String url, String params,Map<String, String> headers) throws Exception {
HttpURLConnection http = null;
if (isHttps(url)) {
http = initHttps(url, _POST, headers);
} else {
http = initHttp(url, _POST, headers);
}
OutputStream out = http.getOutputStream();
out.write(params.getBytes(DEFAULT_CHARSET));
out.flush();
out.close(); InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in,
DEFAULT_CHARSET));
String valueString = null;
StringBuffer bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null) {
bufferRes.append(valueString);
}
in.close();
if (http != null) {
http.disconnect();// 关闭连接
}
return bufferRes.toString();
} /**
 * 功能描述: 构造请求参数
 * 
 * @return 返回类型:
 * @throws Exception
 */
public static String initParams(String url, Map<String, String> params)
throws Exception {
if (null == params || params.isEmpty()) {
return url;
}
StringBuilder sb = new StringBuilder(url);
if (url.indexOf("?") == -1) {
sb.append("?");
}
sb.append(map2Url(params));
return sb.toString();
} /**
 * map构造url
 * 
 * @return 返回类型:
 * @throws Exception
 */
public static String map2Url(Map<String, String> paramToMap)
throws Exception {
if (null == paramToMap || paramToMap.isEmpty()) {
return null;
}
StringBuffer url = new StringBuffer();
boolean isfist = true;
for (Entry<String, String> entry : paramToMap.entrySet()) {
if (isfist) {
isfist = false;
} else {
url.append("&");
}
url.append(entry.getKey()).append("=");
String value = entry.getValue();
if (!StringUtils.isEmpty(value)) {
url.append(URLEncoder.encode(value, DEFAULT_CHARSET));
}
}
return url.toString();
} /**
 * 检测是否https
 * 
 * @param url
 */
private static boolean isHttps(String url) {
return url.startsWith("https");
} /**
 * https 域名校验
 * 
 * @param url
 * @param params
 * @return
 */
public class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;// 直接返回true
}
} private static class MyX509TrustManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
} public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
} public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}; public static void main(String[] args) {
String str = "";
try {
str = get("https://www.baidu.com");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println(str);
} }

最新文章

  1. Atitit xml命名空间机制
  2. Thinkphp5简单初体验
  3. BigInger isProbablePrime
  4. ubuntu11.10搭建eclipse C++开发环境[zhuan]
  5. HP StorageWorks MSL2024 Tape Libraries - Tape library Error Codes
  6. java finally中含return语句
  7. new 、operator new 和 placement new
  8. linux 下载软件
  9. Oracle EBS 预警系统管理
  10. C/C++捕获段错误,打印出错的具体位置(精确到哪一行)
  11. OCP-1Z0-051-题目解析-第1题
  12. ios runtime部分事例方法说明
  13. Angular2.js——主从结构
  14. MQTT报文格式
  15. [IntelliJ IDEA入门] 新建一个Java项目
  16. python之WebSocket协议
  17. Leetcode 63
  18. Kubernetes busybox nslookup问题
  19. openwrt: Makefile 框架分析[转载]
  20. ubuntu 15.04默认root用户登陆

热门文章

  1. 一文学会Java死锁和CPU 100% 问题的排查技巧
  2. Python爬虫的起点
  3. C++ 洛谷 P2657 [SCOI2009]windy数 题解
  4. Parameters.AddWithValue(“@参数”,value)方法
  5. zeroMQ 4 java
  6. 前端从零开始学习Graphql
  7. Bzoj 2318 Spoj4060 game with probability Problem
  8. ~~小练习:python的简易购物车~~
  9. 使用gulp构建微信小程序工作流
  10. 重新认识 async/await 语法糖