|
|
@@ -32,8 +32,12 @@ public class HttpsUtils {
|
|
|
* @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()};
|
|
|
@@ -44,7 +48,7 @@ public class HttpsUtils {
|
|
|
|
|
|
//url是https的时候,使用异常更正
|
|
|
URL url = new URL(null, requestUrl, new Handler());
|
|
|
- HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
/** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
httpUrlConn.setConnectTimeout(15000);
|
|
|
/** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
@@ -72,27 +76,43 @@ public class HttpsUtils {
|
|
|
outputStream.close();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 将返回的输入流转换成字符串
|
|
|
- InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
-
|
|
|
- String str = null;
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
- buffer.append(str);
|
|
|
+ /** 请求成功:返回码为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);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 释放资源
|
|
|
- bufferedReader.close();
|
|
|
- inputStreamReader.close();
|
|
|
- inputStream.close();
|
|
|
- httpUrlConn.disconnect();
|
|
|
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;
|
|
|
@@ -108,8 +128,12 @@ public class HttpsUtils {
|
|
|
* @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()};
|
|
|
@@ -120,7 +144,7 @@ public class HttpsUtils {
|
|
|
|
|
|
//url是https的时候,使用异常更正
|
|
|
URL url = new URL(null, requestUrl, new Handler());
|
|
|
- HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
/** 设置连接主机服务器超时时间:15000毫秒 */
|
|
|
httpUrlConn.setConnectTimeout(15000);
|
|
|
/** 设置读取远程返回的数据时间:60000毫秒 */
|
|
|
@@ -152,26 +176,43 @@ public class HttpsUtils {
|
|
|
outputStream.close();
|
|
|
}
|
|
|
}
|
|
|
- // 将返回的输入流转换成字符串
|
|
|
- InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
-
|
|
|
- String str = null;
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
- buffer.append(str);
|
|
|
+ /** 请求成功:返回码为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);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 释放资源
|
|
|
- bufferedReader.close();
|
|
|
- inputStreamReader.close();
|
|
|
- inputStream.close();
|
|
|
- httpUrlConn.disconnect();
|
|
|
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;
|
|
|
@@ -284,6 +325,9 @@ public class HttpsUtils {
|
|
|
if (null != is) {
|
|
|
is.close();
|
|
|
}
|
|
|
+ if (null != os) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
@@ -291,7 +335,7 @@ public class HttpsUtils {
|
|
|
// 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
// 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
connection.disconnect();
|
|
|
- System.out.println("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
}
|
|
|
jsonObject = JSONObject.parseObject(result);
|
|
|
return jsonObject;
|
|
|
@@ -386,6 +430,9 @@ public class HttpsUtils {
|
|
|
if (null != is) {
|
|
|
is.close();
|
|
|
}
|
|
|
+ if (null != os) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
@@ -393,7 +440,7 @@ public class HttpsUtils {
|
|
|
// 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
|
|
|
// 固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些
|
|
|
connection.disconnect();
|
|
|
- System.out.println("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
+ log.info("--------->>> " + requestMethod + " request end <<<----------");
|
|
|
}
|
|
|
jsonObject = JSONObject.parseObject(result);
|
|
|
return jsonObject;
|