|
|
@@ -1,6 +1,7 @@
|
|
|
package com.railway.common.utils.http;
|
|
|
|
|
|
import com.railway.common.constant.Constants;
|
|
|
+import com.railway.common.utils.StringUtils;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
@@ -50,26 +51,17 @@ public class HttpUtils {
|
|
|
public static String sendGet(String url, String param, String contentType) {
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
BufferedReader in = null;
|
|
|
+ InputStream is = sendGetStream(url, param);
|
|
|
+ if (null == is) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
try {
|
|
|
- String urlNameString = url + "?" + param;
|
|
|
- log.info("sendGet - {}", urlNameString);
|
|
|
- URL realUrl = new URL(urlNameString);
|
|
|
- URLConnection connection = realUrl.openConnection();
|
|
|
- connection.setRequestProperty("accept", "*/*");
|
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
- connection.setRequestProperty("user-agent",
|
|
|
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
- connection.connect();
|
|
|
- in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
|
|
+ in = new BufferedReader(new InputStreamReader(is, contentType));
|
|
|
String line;
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
result.append(line);
|
|
|
}
|
|
|
log.info("recv - {}", result);
|
|
|
- } catch (ConnectException e) {
|
|
|
- log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
- } catch (SocketTimeoutException e) {
|
|
|
- log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
} catch (IOException e) {
|
|
|
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
} catch (Exception e) {
|
|
|
@@ -86,6 +78,15 @@ public class HttpUtils {
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 向指定 URL 发送GET方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的 URL
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
+ */
|
|
|
+ public static InputStream sendGetStream(String url) {
|
|
|
+ return sendGetStream(url, null);
|
|
|
+ }
|
|
|
/**
|
|
|
* 向指定 URL 发送GET方法的请求
|
|
|
*
|
|
|
@@ -94,20 +95,20 @@ public class HttpUtils {
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
public static InputStream sendGetStream(String url, String param) {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- BufferedReader in = null;
|
|
|
+ String urlNameString = url;
|
|
|
+ if (StringUtils.isNotEmpty(param)) {
|
|
|
+ urlNameString = url + "?" + param;
|
|
|
+ }
|
|
|
+ log.info("sendGet - {}", urlNameString);
|
|
|
try {
|
|
|
- String urlNameString = url + "?" + param;
|
|
|
- log.info("sendGet - {}", urlNameString);
|
|
|
URL realUrl = new URL(urlNameString);
|
|
|
URLConnection connection = realUrl.openConnection();
|
|
|
connection.setRequestProperty("accept", "*/*");
|
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
connection.setRequestProperty("user-agent",
|
|
|
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
connection.connect();
|
|
|
- InputStream inputStream = connection.getInputStream();
|
|
|
- return inputStream;
|
|
|
+ return connection.getInputStream();
|
|
|
} catch (ConnectException e) {
|
|
|
log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
@@ -116,14 +117,6 @@ public class HttpUtils {
|
|
|
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (in != null) {
|
|
|
- in.close();
|
|
|
- }
|
|
|
- } catch (Exception ex) {
|
|
|
- log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
|
- }
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -147,7 +140,7 @@ public class HttpUtils {
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
conn.setRequestProperty("user-agent",
|
|
|
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
|
|
conn.setRequestProperty("contentType", "utf-8");
|
|
|
conn.setDoOutput(true);
|
|
|
@@ -191,13 +184,13 @@ public class HttpUtils {
|
|
|
log.info("sendSSLPost - {}", urlNameString);
|
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
|
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
|
|
|
- new java.security.SecureRandom());
|
|
|
+ new java.security.SecureRandom());
|
|
|
URL console = new URL(urlNameString);
|
|
|
HttpsURLConnection conn = (HttpsURLConnection) console.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)");
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
|
|
conn.setRequestProperty("contentType", "utf-8");
|
|
|
conn.setDoOutput(true);
|
|
|
@@ -212,7 +205,7 @@ public class HttpUtils {
|
|
|
while ((ret = br.readLine()) != null) {
|
|
|
if (!"".equals(ret.trim())) {
|
|
|
result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1),
|
|
|
- StandardCharsets.UTF_8));
|
|
|
+ StandardCharsets.UTF_8));
|
|
|
}
|
|
|
}
|
|
|
log.info("recv - {}", result);
|
|
|
@@ -222,7 +215,7 @@ public class HttpUtils {
|
|
|
log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param,
|
|
|
- e);
|
|
|
+ e);
|
|
|
} catch (IOException e) {
|
|
|
log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
|
|
|
} catch (Exception e) {
|