|
|
@@ -1,448 +1,448 @@
|
|
|
-package org.jeecg.modules.util;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import sun.net.www.protocol.https.Handler;
|
|
|
-
|
|
|
-import javax.net.ssl.*;
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.net.ConnectException;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.security.cert.X509Certificate;
|
|
|
-
|
|
|
-/**
|
|
|
- * @Author: zwq
|
|
|
- * @Date: Create in 2020/8/12 14:18
|
|
|
- * @Description: https请求工具类
|
|
|
- */
|
|
|
-public class HttpsUtils {
|
|
|
- private static Logger log = LoggerFactory.getLogger(HttpsUtils.class);
|
|
|
-
|
|
|
- /**
|
|
|
- * 发起https请求并获取结果
|
|
|
- *
|
|
|
- * @param requestUrl 请求地址
|
|
|
- * @param requestMethod 请求方式(GET、post)
|
|
|
- * @param outputStr 提交的数据
|
|
|
- * @return json字符串
|
|
|
- */
|
|
|
- public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
- HttpsURLConnection httpUrlConn = null;
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
- JSONObject jsonObject;
|
|
|
- InputStream inputStream = null;
|
|
|
- InputStreamReader inputStreamReader = null;
|
|
|
- BufferedReader bufferedReader = null;
|
|
|
- try {
|
|
|
- // 创建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是https的时候,使用异常更正
|
|
|
- URL url = new URL(null, requestUrl, new Handler());
|
|
|
- httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
- /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
- httpUrlConn.setConnectTimeout(15000);
|
|
|
- /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
- httpUrlConn.setReadTimeout(60000);
|
|
|
- httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
-
|
|
|
- /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- httpUrlConn.setDoOutput(true);
|
|
|
- httpUrlConn.setDoInput(true);
|
|
|
- }
|
|
|
- httpUrlConn.setUseCaches(false);
|
|
|
- // 设置请求方式(GET/POST)
|
|
|
- httpUrlConn.setRequestMethod(requestMethod);
|
|
|
- /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
- if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
- httpUrlConn.connect();
|
|
|
- }
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- //当outputStr不为null时向输出流写数据
|
|
|
- if (null != outputStr) {
|
|
|
- OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
- }
|
|
|
- /** 请求成功:返回码为200 */
|
|
|
- if (httpUrlConn.getResponseCode() == 200) {
|
|
|
- // 将返回的输入流转换成字符串
|
|
|
- inputStream = httpUrlConn.getInputStream();
|
|
|
- inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
- bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
-
|
|
|
- String str = null;
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
- buffer.append(str);
|
|
|
- }
|
|
|
- }
|
|
|
- log.debug("https buffer:" + buffer.toString());
|
|
|
- } catch (ConnectException ce) {
|
|
|
- log.error("server connection timed out");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- /** 关闭资源 */
|
|
|
- try {
|
|
|
- if (null != bufferedReader) {
|
|
|
- bufferedReader.close();
|
|
|
- }
|
|
|
- if (null != inputStreamReader) {
|
|
|
- inputStreamReader.close();
|
|
|
- }
|
|
|
- if (null != inputStream) {
|
|
|
- inputStream.close();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- /** 关闭远程连接 */
|
|
|
- // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
- // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
- httpUrlConn.disconnect();
|
|
|
- log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
- }
|
|
|
- jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发起https请求并获取结果
|
|
|
- *
|
|
|
- * @param requestUrl 请求地址
|
|
|
- * @param requestMethod 请求方式(GET、post)
|
|
|
- * @param outputStr 提交的数据
|
|
|
- * @param token 鉴权accessToken
|
|
|
- * @return json字符串
|
|
|
- */
|
|
|
- public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr, String token) {
|
|
|
- HttpsURLConnection httpUrlConn = null;
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
- JSONObject jsonObject;
|
|
|
- InputStream inputStream = null;
|
|
|
- InputStreamReader inputStreamReader = null;
|
|
|
- BufferedReader bufferedReader = null;
|
|
|
- try {
|
|
|
- // 创建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是https的时候,使用异常更正
|
|
|
- URL url = new URL(null, requestUrl, new Handler());
|
|
|
- httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
- /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
- httpUrlConn.setConnectTimeout(15000);
|
|
|
- /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
- httpUrlConn.setReadTimeout(60000);
|
|
|
- httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
-
|
|
|
- /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- httpUrlConn.setDoOutput(true);
|
|
|
- httpUrlConn.setDoInput(true);
|
|
|
- }
|
|
|
- httpUrlConn.setUseCaches(false);
|
|
|
- // 设置请求方式(GET/POST)
|
|
|
- httpUrlConn.setRequestMethod(requestMethod);
|
|
|
- //设置Authorization授权token
|
|
|
- httpUrlConn.setRequestProperty("Authorization", "bearer" + token);
|
|
|
- httpUrlConn.setRequestProperty("Content-type", "application/json");
|
|
|
-
|
|
|
- /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
- if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
- httpUrlConn.connect();
|
|
|
- }
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- //当outputStr不为null时向输出流写数据
|
|
|
- if (null != outputStr) {
|
|
|
- OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
- }
|
|
|
- /** 请求成功:返回码为200 */
|
|
|
- if (httpUrlConn.getResponseCode() == 200) {
|
|
|
- // 将返回的输入流转换成字符串
|
|
|
- inputStream = httpUrlConn.getInputStream();
|
|
|
- inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
- bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
-
|
|
|
- String str = null;
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
- buffer.append(str);
|
|
|
- }
|
|
|
- }
|
|
|
- log.debug("https buffer:" + buffer.toString());
|
|
|
- } catch (ConnectException ce) {
|
|
|
- log.error("server connection timed out");
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- /** 关闭资源 */
|
|
|
- try {
|
|
|
- if (null != bufferedReader) {
|
|
|
- bufferedReader.close();
|
|
|
- }
|
|
|
- if (null != inputStreamReader) {
|
|
|
- inputStreamReader.close();
|
|
|
- }
|
|
|
- if (null != inputStream) {
|
|
|
- inputStream.close();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- /** 关闭远程连接 */
|
|
|
- // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
- // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
- httpUrlConn.disconnect();
|
|
|
- log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
- }
|
|
|
- jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 证书信任管理器(用于https请求)
|
|
|
- */
|
|
|
- static class MyX509TrustManager implements X509TrustManager {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public X509Certificate[] getAcceptedIssuers() {
|
|
|
- return new X509Certificate[0];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 发起http请求并获取结果
|
|
|
- *
|
|
|
- * @param requestUrl 请求地址
|
|
|
- * @param requestMethod 请求方式(GET、post)
|
|
|
- * @param param 提交的数据
|
|
|
- * @return json字符串
|
|
|
- */
|
|
|
- public static JSONObject doHttpRequest(String requestUrl, String requestMethod, String param) {
|
|
|
- HttpURLConnection connection = null;
|
|
|
- InputStream is = null;
|
|
|
- OutputStream os = null;
|
|
|
- BufferedReader br = null;
|
|
|
- String result = null;
|
|
|
- JSONObject jsonObject;
|
|
|
- try {
|
|
|
- /** 创建远程url连接对象 */
|
|
|
- URL url = new URL(requestUrl);
|
|
|
-
|
|
|
- /** 通过远程url对象打开一个连接,强制转换为HttpUrlConnection类型 */
|
|
|
- connection = (HttpURLConnection) url.openConnection();
|
|
|
- /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
- connection.setConnectTimeout(15000);
|
|
|
- /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
- connection.setReadTimeout(60000);
|
|
|
-
|
|
|
- /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
|
|
- connection.setDoOutput(true);
|
|
|
- // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
|
|
- connection.setDoInput(true);
|
|
|
- }
|
|
|
- connection.setUseCaches(false);
|
|
|
- /** 设置连接方式:GET/POST */
|
|
|
- connection.setRequestMethod(requestMethod);
|
|
|
- /** 设置通用的请求属性 */
|
|
|
- connection.setRequestProperty("accept", "*/*");
|
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
- connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
-
|
|
|
- /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
- if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
- connection.connect();
|
|
|
- }
|
|
|
-
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- /** 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 */
|
|
|
- // 若使用os.print(param);则需要释放缓存:os.flush();即使用字符流输出需要释放缓存,字节流则不需要
|
|
|
- if (param != null && param.length() > 0) {
|
|
|
- /** 通过连接对象获取一个输出流 */
|
|
|
- os = connection.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- os.write(param.getBytes("UTF-8"));
|
|
|
- os.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /** 请求成功:返回码为200 */
|
|
|
- if (connection.getResponseCode() == 200) {
|
|
|
- /** 通过连接对象获取一个输入流,向远程读取 */
|
|
|
- is = connection.getInputStream();
|
|
|
- /** 封装输入流is,并指定字符集 */
|
|
|
- br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
-
|
|
|
- StringBuffer sbf = new StringBuffer();
|
|
|
- String line = null;
|
|
|
- while ((line = br.readLine()) != null) {
|
|
|
- sbf.append(line);
|
|
|
- sbf.append("\r\n");
|
|
|
- }
|
|
|
- result = sbf.toString();
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- /** 关闭资源 */
|
|
|
- try {
|
|
|
- if (null != br) {
|
|
|
- br.close();
|
|
|
- }
|
|
|
- if (null != is) {
|
|
|
- is.close();
|
|
|
- }
|
|
|
- if (null != os) {
|
|
|
- os.close();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- /** 关闭远程连接 */
|
|
|
- // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
- // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
- connection.disconnect();
|
|
|
- log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
- }
|
|
|
- jsonObject = JSONObject.parseObject(result);
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发起http请求并获取结果
|
|
|
- *
|
|
|
- * @param requestUrl 请求地址
|
|
|
- * @param requestMethod 请求方式(GET、post)
|
|
|
- * @param param 提交的数据
|
|
|
- * @param token 鉴权accessToken
|
|
|
- * @return json字符串
|
|
|
- */
|
|
|
- public static JSONObject doHttpRequest(String requestUrl, String requestMethod, String param, String token) {
|
|
|
- HttpURLConnection connection = null;
|
|
|
- InputStream is = null;
|
|
|
- OutputStream os = null;
|
|
|
- BufferedReader br = null;
|
|
|
- String result = null;
|
|
|
- JSONObject jsonObject;
|
|
|
- try {
|
|
|
- /** 创建远程url连接对象 */
|
|
|
- URL url = new URL(requestUrl);
|
|
|
-
|
|
|
- /** 通过远程url对象打开一个连接,强制转换为HttpUrlConnection类型 */
|
|
|
- connection = (HttpURLConnection) url.openConnection();
|
|
|
- /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
- connection.setConnectTimeout(15000);
|
|
|
- /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
- connection.setReadTimeout(60000);
|
|
|
-
|
|
|
- /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
|
|
- connection.setDoOutput(true);
|
|
|
- // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
|
|
- connection.setDoInput(true);
|
|
|
- }
|
|
|
- connection.setUseCaches(false);
|
|
|
- /** 设置连接方式:GET/POST */
|
|
|
- connection.setRequestMethod(requestMethod);
|
|
|
- /** 设置通用的请求属性 */
|
|
|
- connection.setRequestProperty("accept", "*/*");
|
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
- connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
- //设置Authorization授权token
|
|
|
- connection.setRequestProperty("Authorization", "bearer" + token);
|
|
|
- connection.setRequestProperty("Content-type", "application/json");
|
|
|
-
|
|
|
- /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
- if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
- connection.connect();
|
|
|
- }
|
|
|
-
|
|
|
- if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
- /** 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 */
|
|
|
- // 若使用os.print(param);则需要释放缓存:os.flush();即使用字符流输出需要释放缓存,字节流则不需要
|
|
|
- if (param != null && param.length() > 0) {
|
|
|
- /** 通过连接对象获取一个输出流 */
|
|
|
- os = connection.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- os.write(param.getBytes("UTF-8"));
|
|
|
- os.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /** 请求成功:返回码为200 */
|
|
|
- if (connection.getResponseCode() == 200) {
|
|
|
- /** 通过连接对象获取一个输入流,向远程读取 */
|
|
|
- is = connection.getInputStream();
|
|
|
- /** 封装输入流is,并指定字符集 */
|
|
|
- br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
-
|
|
|
- StringBuffer sbf = new StringBuffer();
|
|
|
- String line = null;
|
|
|
- while ((line = br.readLine()) != null) {
|
|
|
- sbf.append(line);
|
|
|
- sbf.append("\r\n");
|
|
|
- }
|
|
|
- result = sbf.toString();
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- /** 关闭资源 */
|
|
|
- try {
|
|
|
- if (null != br) {
|
|
|
- br.close();
|
|
|
- }
|
|
|
- if (null != is) {
|
|
|
- is.close();
|
|
|
- }
|
|
|
- if (null != os) {
|
|
|
- os.close();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- /** 关闭远程连接 */
|
|
|
- // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
- // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
- connection.disconnect();
|
|
|
- log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
- }
|
|
|
- jsonObject = JSONObject.parseObject(result);
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-}
|
|
|
+package org.jeecg.common.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import sun.net.www.protocol.https.Handler;
|
|
|
+
|
|
|
+import javax.net.ssl.*;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.ConnectException;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: zwq
|
|
|
+ * @Date: Create in 2020/8/12 14:18
|
|
|
+ * @Description: https请求工具类
|
|
|
+ */
|
|
|
+public class HttpsUtils {
|
|
|
+ private static Logger log = LoggerFactory.getLogger(HttpsUtils.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发起https请求并获取结果
|
|
|
+ *
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param outputStr 提交的数据
|
|
|
+ * @return json字符串
|
|
|
+ */
|
|
|
+ public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
+ HttpsURLConnection httpUrlConn = null;
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ JSONObject jsonObject;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ InputStreamReader inputStreamReader = null;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ // 创建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是https的时候,使用异常更正
|
|
|
+ URL url = new URL(null, requestUrl, new Handler());
|
|
|
+ httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ httpUrlConn.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ httpUrlConn.setReadTimeout(60000);
|
|
|
+ httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
+
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ }
|
|
|
+ httpUrlConn.setUseCaches(false);
|
|
|
+ // 设置请求方式(GET/POST)
|
|
|
+ httpUrlConn.setRequestMethod(requestMethod);
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.connect();
|
|
|
+ }
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ //当outputStr不为null时向输出流写数据
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /** 请求成功:返回码为200 */
|
|
|
+ if (httpUrlConn.getResponseCode() == 200) {
|
|
|
+ // 将返回的输入流转换成字符串
|
|
|
+ inputStream = httpUrlConn.getInputStream();
|
|
|
+ inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
+ bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+
|
|
|
+ String str = null;
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.debug("https buffer:" + buffer.toString());
|
|
|
+ } catch (ConnectException ce) {
|
|
|
+ log.error("server connection timed out");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ /** 关闭资源 */
|
|
|
+ try {
|
|
|
+ if (null != bufferedReader) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ if (null != inputStreamReader) {
|
|
|
+ inputStreamReader.close();
|
|
|
+ }
|
|
|
+ if (null != inputStream) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ httpUrlConn.disconnect();
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ }
|
|
|
+ jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发起https请求并获取结果
|
|
|
+ *
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param outputStr 提交的数据
|
|
|
+ * @param token 鉴权accessToken
|
|
|
+ * @return json字符串
|
|
|
+ */
|
|
|
+ public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr, String token) {
|
|
|
+ HttpsURLConnection httpUrlConn = null;
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ JSONObject jsonObject;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ InputStreamReader inputStreamReader = null;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ // 创建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是https的时候,使用异常更正
|
|
|
+ URL url = new URL(null, requestUrl, new Handler());
|
|
|
+ httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ httpUrlConn.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ httpUrlConn.setReadTimeout(60000);
|
|
|
+ httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
+
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ }
|
|
|
+ httpUrlConn.setUseCaches(false);
|
|
|
+ // 设置请求方式(GET/POST)
|
|
|
+ httpUrlConn.setRequestMethod(requestMethod);
|
|
|
+ //设置Authorization授权token
|
|
|
+ httpUrlConn.setRequestProperty("Authorization", "bearer" + token);
|
|
|
+ httpUrlConn.setRequestProperty("Content-type", "application/json");
|
|
|
+
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.connect();
|
|
|
+ }
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ //当outputStr不为null时向输出流写数据
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /** 请求成功:返回码为200 */
|
|
|
+ if (httpUrlConn.getResponseCode() == 200) {
|
|
|
+ // 将返回的输入流转换成字符串
|
|
|
+ inputStream = httpUrlConn.getInputStream();
|
|
|
+ inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
+ bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+
|
|
|
+ String str = null;
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.debug("https buffer:" + buffer.toString());
|
|
|
+ } catch (ConnectException ce) {
|
|
|
+ log.error("server connection timed out");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ /** 关闭资源 */
|
|
|
+ try {
|
|
|
+ if (null != bufferedReader) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ if (null != inputStreamReader) {
|
|
|
+ inputStreamReader.close();
|
|
|
+ }
|
|
|
+ if (null != inputStream) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ httpUrlConn.disconnect();
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ }
|
|
|
+ jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 证书信任管理器(用于https请求)
|
|
|
+ */
|
|
|
+ static class MyX509TrustManager implements X509TrustManager {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return new X509Certificate[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发起http请求并获取结果
|
|
|
+ *
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param param 提交的数据
|
|
|
+ * @return json字符串
|
|
|
+ */
|
|
|
+ public static JSONObject doHttpRequest(String requestUrl, String requestMethod, String param) {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ InputStream is = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+ String result = null;
|
|
|
+ JSONObject jsonObject;
|
|
|
+ try {
|
|
|
+ /** 创建远程url连接对象 */
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
+
|
|
|
+ /** 通过远程url对象打开一个连接,强制转换为HttpUrlConnection类型 */
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ connection.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ connection.setReadTimeout(60000);
|
|
|
+
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
|
|
+ connection.setDoInput(true);
|
|
|
+ }
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ /** 设置连接方式:GET/POST */
|
|
|
+ connection.setRequestMethod(requestMethod);
|
|
|
+ /** 设置通用的请求属性 */
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
+ connection.connect();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ /** 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 */
|
|
|
+ // 若使用os.print(param);则需要释放缓存:os.flush();即使用字符流输出需要释放缓存,字节流则不需要
|
|
|
+ if (param != null && param.length() > 0) {
|
|
|
+ /** 通过连接对象获取一个输出流 */
|
|
|
+ os = connection.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ os.write(param.getBytes("UTF-8"));
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 请求成功:返回码为200 */
|
|
|
+ if (connection.getResponseCode() == 200) {
|
|
|
+ /** 通过连接对象获取一个输入流,向远程读取 */
|
|
|
+ is = connection.getInputStream();
|
|
|
+ /** 封装输入流is,并指定字符集 */
|
|
|
+ br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
+
|
|
|
+ StringBuffer sbf = new StringBuffer();
|
|
|
+ String line = null;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sbf.append(line);
|
|
|
+ sbf.append("\r\n");
|
|
|
+ }
|
|
|
+ result = sbf.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ /** 关闭资源 */
|
|
|
+ try {
|
|
|
+ if (null != br) {
|
|
|
+ br.close();
|
|
|
+ }
|
|
|
+ if (null != is) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (null != os) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ connection.disconnect();
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ }
|
|
|
+ jsonObject = JSONObject.parseObject(result);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发起http请求并获取结果
|
|
|
+ *
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param param 提交的数据
|
|
|
+ * @param token 鉴权accessToken
|
|
|
+ * @return json字符串
|
|
|
+ */
|
|
|
+ public static JSONObject doHttpRequest(String requestUrl, String requestMethod, String param, String token) {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ InputStream is = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+ String result = null;
|
|
|
+ JSONObject jsonObject;
|
|
|
+ try {
|
|
|
+ /** 创建远程url连接对象 */
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
+
|
|
|
+ /** 通过远程url对象打开一个连接,强制转换为HttpUrlConnection类型 */
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ connection.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ connection.setReadTimeout(60000);
|
|
|
+
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
|
|
+ connection.setDoInput(true);
|
|
|
+ }
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ /** 设置连接方式:GET/POST */
|
|
|
+ connection.setRequestMethod(requestMethod);
|
|
|
+ /** 设置通用的请求属性 */
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ //设置Authorization授权token
|
|
|
+ connection.setRequestProperty("Authorization", "bearer" + token);
|
|
|
+ connection.setRequestProperty("Content-type", "application/json");
|
|
|
+
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
+ connection.connect();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ /** 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 */
|
|
|
+ // 若使用os.print(param);则需要释放缓存:os.flush();即使用字符流输出需要释放缓存,字节流则不需要
|
|
|
+ if (param != null && param.length() > 0) {
|
|
|
+ /** 通过连接对象获取一个输出流 */
|
|
|
+ os = connection.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ os.write(param.getBytes("UTF-8"));
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 请求成功:返回码为200 */
|
|
|
+ if (connection.getResponseCode() == 200) {
|
|
|
+ /** 通过连接对象获取一个输入流,向远程读取 */
|
|
|
+ is = connection.getInputStream();
|
|
|
+ /** 封装输入流is,并指定字符集 */
|
|
|
+ br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
+
|
|
|
+ StringBuffer sbf = new StringBuffer();
|
|
|
+ String line = null;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sbf.append(line);
|
|
|
+ sbf.append("\r\n");
|
|
|
+ }
|
|
|
+ result = sbf.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ /** 关闭资源 */
|
|
|
+ try {
|
|
|
+ if (null != br) {
|
|
|
+ br.close();
|
|
|
+ }
|
|
|
+ if (null != is) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (null != os) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ connection.disconnect();
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ }
|
|
|
+ jsonObject = JSONObject.parseObject(result);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+}
|