|
|
@@ -6,13 +6,14 @@ import org.slf4j.LoggerFactory;
|
|
|
import sun.net.www.protocol.https.Handler;
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
-import java.io.*;
|
|
|
+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.net.URLConnection;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Author: zwq
|
|
|
@@ -30,7 +31,7 @@ public class HttpsUtils {
|
|
|
* @param outputStr 提交的数据
|
|
|
* @return json字符串
|
|
|
*/
|
|
|
- public static JSONObject doRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
+ public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
JSONObject jsonObject;
|
|
|
try {
|
|
|
@@ -44,24 +45,32 @@ public class HttpsUtils {
|
|
|
//url是https的时候,使用异常更正
|
|
|
URL url = new URL(null, requestUrl, new Handler());
|
|
|
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ httpUrlConn.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ httpUrlConn.setReadTimeout(60000);
|
|
|
httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
|
|
|
- httpUrlConn.setDoOutput(true);
|
|
|
- httpUrlConn.setDoInput(true);
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ }
|
|
|
httpUrlConn.setUseCaches(false);
|
|
|
// 设置请求方式(GET/POST)
|
|
|
httpUrlConn.setRequestMethod(requestMethod);
|
|
|
-
|
|
|
- if ("GET".equalsIgnoreCase(requestMethod)) {
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
httpUrlConn.connect();
|
|
|
}
|
|
|
-
|
|
|
- //当outputStr不为null时向输出流写数据
|
|
|
- if (null != outputStr) {
|
|
|
- OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
- outputStream.close();
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ //当outputStr不为null时向输出流写数据
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 将返回的输入流转换成字符串
|
|
|
@@ -95,9 +104,10 @@ public class HttpsUtils {
|
|
|
* @param requestUrl 请求地址
|
|
|
* @param requestMethod 请求方式(GET、post)
|
|
|
* @param outputStr 提交的数据
|
|
|
+ * @param token 鉴权accessToken
|
|
|
* @return json字符串
|
|
|
*/
|
|
|
- public static JSONObject doRequest(String requestUrl, String requestMethod, String outputStr, String token) {
|
|
|
+ public static JSONObject doHttpsRequest(String requestUrl, String requestMethod, String outputStr, String token) {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
JSONObject jsonObject;
|
|
|
try {
|
|
|
@@ -111,10 +121,17 @@ public class HttpsUtils {
|
|
|
//url是https的时候,使用异常更正
|
|
|
URL url = new URL(null, requestUrl, new Handler());
|
|
|
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ /** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
+ httpUrlConn.setConnectTimeout(15000);
|
|
|
+ /** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
+ httpUrlConn.setReadTimeout(60000);
|
|
|
httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
|
|
|
- httpUrlConn.setDoOutput(true);
|
|
|
- httpUrlConn.setDoInput(true);
|
|
|
+ /** 设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个 */
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ }
|
|
|
httpUrlConn.setUseCaches(false);
|
|
|
// 设置请求方式(GET/POST)
|
|
|
httpUrlConn.setRequestMethod(requestMethod);
|
|
|
@@ -122,18 +139,19 @@ public class HttpsUtils {
|
|
|
httpUrlConn.setRequestProperty("Authorization", "bearer" + token);
|
|
|
httpUrlConn.setRequestProperty("Content-type", "application/json");
|
|
|
|
|
|
- if ("GET".equalsIgnoreCase(requestMethod)) {
|
|
|
+ /** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
|
|
|
+ if (HttpsContants.GET.equalsIgnoreCase(requestMethod)) {
|
|
|
httpUrlConn.connect();
|
|
|
}
|
|
|
-
|
|
|
- //当outputStr不为null时向输出流写数据
|
|
|
- if (null != outputStr) {
|
|
|
- OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
- outputStream.close();
|
|
|
+ if (HttpsContants.POST.equalsIgnoreCase(requestMethod)) {
|
|
|
+ //当outputStr不为null时向输出流写数据
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
// 将返回的输入流转换成字符串
|
|
|
InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
@@ -180,124 +198,204 @@ public class HttpsUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * 发起http请求
|
|
|
- * 向指定URL发送POST请求
|
|
|
+ * 发起http请求并获取结果
|
|
|
*
|
|
|
- * @param url
|
|
|
- * @param paramMap
|
|
|
- * @return 响应结果
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param param 提交的数据
|
|
|
+ * @return json字符串
|
|
|
*/
|
|
|
- public static String sendPost(String url, Map<String, String> paramMap) {
|
|
|
- PrintWriter out = null;
|
|
|
- BufferedReader in = null;
|
|
|
- String result = "";
|
|
|
+ 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 realUrl = new URL(url);
|
|
|
- URLConnection conn = realUrl.openConnection();
|
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
- conn.setRequestProperty("Charset", "UTF-8");
|
|
|
- conn.setDoOutput(true);
|
|
|
- conn.setDoInput(true);
|
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
|
-
|
|
|
- String param = "";
|
|
|
- if (paramMap != null && paramMap.size() > 0) {
|
|
|
- Iterator<String> ite = paramMap.keySet().iterator();
|
|
|
- while (ite.hasNext()) {
|
|
|
- String key = ite.next();
|
|
|
- String value = paramMap.get(key);
|
|
|
- param += key + "=" + value + "&";
|
|
|
+ /** 创建远程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();
|
|
|
}
|
|
|
- param = param.substring(0, param.length() - 1);
|
|
|
}
|
|
|
|
|
|
- out.print(param);
|
|
|
- out.flush();
|
|
|
- in = new BufferedReader(
|
|
|
- new InputStreamReader(conn.getInputStream()));
|
|
|
- String line;
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- result += line;
|
|
|
+ /** 请求成功:返回码为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 (out != null) {
|
|
|
- out.close();
|
|
|
+ if (null != br) {
|
|
|
+ br.close();
|
|
|
}
|
|
|
- if (in != null) {
|
|
|
- in.close();
|
|
|
+ if (null != is) {
|
|
|
+ is.close();
|
|
|
}
|
|
|
- } catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ connection.disconnect();
|
|
|
+ System.out.println("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
}
|
|
|
- return result;
|
|
|
+ jsonObject = JSONObject.parseObject(result);
|
|
|
+ return jsonObject;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发起http请求
|
|
|
- * 向指定URL发送POST请求
|
|
|
+ * 发起http请求并获取结果
|
|
|
*
|
|
|
- * @param url
|
|
|
- * @param paramMap
|
|
|
- * @return 响应结果
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param requestMethod 请求方式(GET、post)
|
|
|
+ * @param param 提交的数据
|
|
|
+ * @param token 鉴权accessToken
|
|
|
+ * @return json字符串
|
|
|
*/
|
|
|
- public static String sendPost(String url, Map<String, String> paramMap, String token) {
|
|
|
- PrintWriter out = null;
|
|
|
- BufferedReader in = null;
|
|
|
- String result = "";
|
|
|
+ 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 realUrl = new URL(url);
|
|
|
- URLConnection conn = realUrl.openConnection();
|
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
- conn.setRequestProperty("Charset", "UTF-8");
|
|
|
+ /** 创建远程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
|
|
|
- conn.setRequestProperty("Authorization", "bearer" + token);
|
|
|
- conn.setRequestProperty("Content-type", "application/json");
|
|
|
- conn.setDoOutput(true);
|
|
|
- conn.setDoInput(true);
|
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
|
-
|
|
|
- String param = "";
|
|
|
- if (paramMap != null && paramMap.size() > 0) {
|
|
|
- Iterator<String> ite = paramMap.keySet().iterator();
|
|
|
- while (ite.hasNext()) {
|
|
|
- String key = ite.next();
|
|
|
- String value = paramMap.get(key);
|
|
|
- param += key + "=" + value + "&";
|
|
|
+ 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();
|
|
|
}
|
|
|
- param = param.substring(0, param.length() - 1);
|
|
|
}
|
|
|
|
|
|
- out.print(param);
|
|
|
- out.flush();
|
|
|
- in = new BufferedReader(
|
|
|
- new InputStreamReader(conn.getInputStream()));
|
|
|
- String line;
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- result += line;
|
|
|
+ /** 请求成功:返回码为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 (out != null) {
|
|
|
- out.close();
|
|
|
+ if (null != br) {
|
|
|
+ br.close();
|
|
|
}
|
|
|
- if (in != null) {
|
|
|
- in.close();
|
|
|
+ if (null != is) {
|
|
|
+ is.close();
|
|
|
}
|
|
|
- } catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ /** 关闭远程连接 */
|
|
|
+ // 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
+ // 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
+ connection.disconnect();
|
|
|
+ System.out.println("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
}
|
|
|
- return result;
|
|
|
+ jsonObject = JSONObject.parseObject(result);
|
|
|
+ return jsonObject;
|
|
|
}
|
|
|
}
|