|
@@ -23,7 +23,7 @@ import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
|
/**
|
|
|
* 通用http发送方法
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
public class HttpUtils
|
|
@@ -50,7 +50,7 @@ public class HttpUtils
|
|
|
*/
|
|
|
public static String sendGet(String url, String param)
|
|
|
{
|
|
|
- return sendGet(url, param, Constants.UTF8);
|
|
|
+ return sendGet1(url, param, Constants.UTF8);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -116,6 +116,69 @@ public class HttpUtils
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 向指定 URL 发送GET方法的请求
|
|
|
+ *
|
|
|
+ * @param url 发送请求的 URL
|
|
|
+ * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
+ * @param contentType 编码类型
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
+ */
|
|
|
+ public static String sendGet1(String url, String param, String contentType)
|
|
|
+ {
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ BufferedReader in = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ String urlNameString = StringUtils.isNotBlank(param) ? url + param : url;
|
|
|
+ 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));
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ 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 result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 向指定 URL 发送POST方法的请求
|
|
|
*
|
|
@@ -271,4 +334,4 @@ public class HttpUtils
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|