|
@@ -1,7 +1,6 @@
|
|
package com.lantone.common.util;
|
|
package com.lantone.common.util;
|
|
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
@@ -23,10 +22,9 @@ import java.util.Map;
|
|
* @author: gaodm
|
|
* @author: gaodm
|
|
* @time: 2018/8/3 17:45
|
|
* @time: 2018/8/3 17:45
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
public class HttpUtils {
|
|
public class HttpUtils {
|
|
|
|
|
|
- private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取当前请求的HttpServletRequest实例
|
|
* 获取当前请求的HttpServletRequest实例
|
|
*
|
|
*
|
|
@@ -120,25 +118,23 @@ public class HttpUtils {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
|
|
|
|
|
+ return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 向指定 URL 发送POST方法的请求
|
|
* 向指定 URL 发送POST方法的请求
|
|
*
|
|
*
|
|
- * @param url 发送请求的 URL
|
|
|
|
|
|
+ * @param url 发送请求的 URL
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
* @return 所代表远程资源的响应结果
|
|
* @return 所代表远程资源的响应结果
|
|
*/
|
|
*/
|
|
- public static String sendPost(String url, String param)
|
|
|
|
- {
|
|
|
|
|
|
+ public static String sendPost(String url, String param) {
|
|
PrintWriter out = null;
|
|
PrintWriter out = null;
|
|
BufferedReader in = null;
|
|
BufferedReader in = null;
|
|
StringBuilder result = new StringBuilder();
|
|
StringBuilder result = new StringBuilder();
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ try {
|
|
String urlNameString = url;
|
|
String urlNameString = url;
|
|
- log.info("sendPost - {}", urlNameString);
|
|
|
|
|
|
+ log.debug("sendPost - {}", urlNameString);
|
|
URL realUrl = new URL(urlNameString);
|
|
URL realUrl = new URL(urlNameString);
|
|
URLConnection conn = realUrl.openConnection();
|
|
URLConnection conn = realUrl.openConnection();
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("accept", "*/*");
|
|
@@ -153,43 +149,27 @@ public class HttpUtils {
|
|
out.flush();
|
|
out.flush();
|
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
|
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
|
|
String line;
|
|
String line;
|
|
- while ((line = in.readLine()) != null)
|
|
|
|
- {
|
|
|
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
result.append(line);
|
|
result.append(line);
|
|
}
|
|
}
|
|
- log.info("recv - {}", result);
|
|
|
|
- }
|
|
|
|
- catch (ConnectException e)
|
|
|
|
- {
|
|
|
|
|
|
+ log.debug("recv - {}", result);
|
|
|
|
+ } catch (ConnectException e) {
|
|
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
- }
|
|
|
|
- catch (SocketTimeoutException e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (SocketTimeoutException e) {
|
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
- }
|
|
|
|
- catch (IOException e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (IOException e) {
|
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
|
- }
|
|
|
|
- finally
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- if (out != null)
|
|
|
|
- {
|
|
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ if (out != null) {
|
|
out.close();
|
|
out.close();
|
|
}
|
|
}
|
|
- if (in != null)
|
|
|
|
- {
|
|
|
|
|
|
+ if (in != null) {
|
|
in.close();
|
|
in.close();
|
|
}
|
|
}
|
|
- }
|
|
|
|
- catch (IOException ex)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (IOException ex) {
|
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
}
|
|
}
|
|
}
|
|
}
|