|
@@ -1,6 +1,7 @@
|
|
|
package com.project.common.utils.http;
|
|
|
|
|
|
import com.project.common.constant.Constants;
|
|
|
+import com.project.common.exception.ServiceException;
|
|
|
import com.project.common.utils.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -59,6 +60,10 @@ public class HttpUtils {
|
|
|
log.info("sendGet - {}", urlNameString);
|
|
|
URL realUrl = new URL(urlNameString);
|
|
|
URLConnection connection = realUrl.openConnection();
|
|
|
+ // 设置连接超时时间为5000毫秒(5秒)
|
|
|
+ connection.setConnectTimeout(5000);
|
|
|
+ // 设置读取超时时间为60000毫秒(60秒)
|
|
|
+ connection.setReadTimeout(60000);
|
|
|
connection.setRequestProperty("accept", "*/*");
|
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
@@ -71,12 +76,16 @@ public class HttpUtils {
|
|
|
log.info("recv - {}", result);
|
|
|
} catch (ConnectException e) {
|
|
|
log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} catch (IOException e) {
|
|
|
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} catch (Exception e) {
|
|
|
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} finally {
|
|
|
try {
|
|
|
if (in != null) {
|
|
@@ -180,13 +189,17 @@ public class HttpUtils {
|
|
|
}
|
|
|
log.info("recv - {}", result);
|
|
|
} catch (ConnectException e) {
|
|
|
- log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ //log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} catch (IOException e) {
|
|
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} catch (Exception e) {
|
|
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
} finally {
|
|
|
try {
|
|
|
if (out != null) {
|