123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package cn.efunbox.base.util;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import java.io.*;
- import java.net.ConnectException;
- import java.net.URL;
- import java.util.UUID;
- public class HttpUtils {
- private static Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);
- public static String doRequest(String requestUrl, String requestMethod, String paramBody) {
- String log = "UUID = "+UUID.randomUUID() + " ; HttpUtils : ";
- LOGGER.info(log + " http weixin api request >>> request = " + requestUrl + " ;paramBody = " + paramBody);
- String result = null;
- try {
- HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
- // 当有数据需要提交时
- if (null != paramBody) {
- OutputStream outputStream = httpUrlConn.getOutputStream();
- // 注意编码格式,防止中文乱码
- outputStream.write(paramBody.getBytes("UTF-8"));
- outputStream.close();
- }
- result = connection2String(httpUrlConn);
- LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;paramBody = " + paramBody + " ;result" + result);
- } catch (ConnectException ce) {
- LOGGER.error(log + "Weixin request { " + requestUrl + " } connection timed out. paramBody : " + paramBody + ce.getMessage(),ce);
- } catch (Exception e) {
- LOGGER.error(log + "http request error:{} requestUrl : " + requestUrl + "paramBody : " + paramBody, e);
- }
- return result;
- }
- public static byte[] doRequestByte(String requestUrl, String requestMethod, String paramBody) {
- String log = "UUID = "+UUID.randomUUID() + " ; HttpUtils : ";
- LOGGER.info(log + " http weixin api request >>> request = " + requestUrl + " ;paramBody = " + paramBody);
- byte[] result = null;
- try {
- HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
- // 当有数据需要提交时
- if (null != paramBody) {
- OutputStream outputStream = httpUrlConn.getOutputStream();
- // 注意编码格式,防止中文乱码
- outputStream.write(paramBody.getBytes("UTF-8"));
- outputStream.close();
- }
- InputStream inputStream = httpUrlConn.getInputStream();
- ByteArrayOutputStream outstream = new ByteArrayOutputStream(httpUrlConn.getContentLength());
- byte[] buffer = new byte[4096];
- int len;
- while ((len = inputStream.read(buffer)) > 0) {
- outstream.write(buffer, 0, len);
- }
- outstream.close();
- result = outstream.toByteArray();
- LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;paramBody = " + paramBody );
- } catch (ConnectException ce) {
- LOGGER.error(log + "Weixin request { " + requestUrl + " } connection timed out. paramBody : " + paramBody + ce.getMessage(),ce);
- } catch (Exception e) {
- LOGGER.error(log + "http request error:{} requestUrl : " + requestUrl + "paramBody : " + paramBody, e);
- }
- return result;
- }
- private static String connection2String(HttpsURLConnection httpUrlConn) {
- StringBuffer buffer = new StringBuffer();
- // 将返回的输入流转换成字符串
- BufferedReader bufferedReader = null;
- try {
- bufferedReader = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream(), "utf-8"));
- String str = null;
- while ((str = bufferedReader.readLine()) != null) {
- buffer.append(str);
- }
- } catch (IOException e) {
- LOGGER.error("HttpsURLConnection2String is error," + e.getMessage(), e);
- } finally {
- if (bufferedReader != null) {
- try {
- bufferedReader.close();
- } catch (IOException e) {
- LOGGER.error("bufferedReader close error," + e.getMessage(), e);
- }
- }
- }
- httpUrlConn.disconnect();
- String result = buffer.toString();
- return result;
- }
- public static String doGet(String requestUrl, String paramBody) {
- return doRequest(requestUrl, "GET", paramBody);
- }
- public static String doPost(String requestUrl, String paramBody) {
- return doRequest(requestUrl, "POST", paramBody);
- }
- public static String doRequest(String requestUrl, String requestMethod) {
- String result = null;
- String log = "UUID = "+UUID.randomUUID();
- LOGGER.info(log + " http weixin api request >>> request = " + requestUrl);
- try {
- HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
- // 将返回的输入流转换成字符串
- result = connection2String(httpUrlConn);
- LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;result" + result);
- } catch (ConnectException ce) {
- LOGGER.error(log + " Weixin request {" + requestUrl + "} connection timed out." + ce.getMessage(),ce);
- } catch (Exception e) {
- LOGGER.error(log + " http request error:{} requestUrl :" + requestUrl + e.getMessage(), e);
- }
- return result;
- }
- private static HttpsURLConnection setSSLContext(String requestUrl,String requestMethod) throws Exception{
- HttpsURLConnection httpUrlConn = null;
- // 创建SSLContext对象,并使用我们指定的信任管理器初始化
- TrustManager[] tm = { new MyX509TrustManager() };
- SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
- sslContext.init(null, tm, new java.security.SecureRandom());
- // 从上述SSLContext对象中得到SSLSocketFactory对象
- SSLSocketFactory ssf = sslContext.getSocketFactory();
- URL url = new URL(requestUrl);
- httpUrlConn = (HttpsURLConnection) url.openConnection();
- httpUrlConn.setSSLSocketFactory(ssf);
- httpUrlConn.setDoOutput(true);
- httpUrlConn.setDoInput(true);
- httpUrlConn.setUseCaches(false);
- // 设置请求方式(GET/POST)
- httpUrlConn.setRequestMethod(requestMethod);
- if ("GET".equalsIgnoreCase(requestMethod)) {
- httpUrlConn.connect();
- }
- return httpUrlConn;
- }
- }
|