update ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java.

public static String sendGet(String url, String param, String contentType)和
public static String sendPost(String url, String param, String contentType)方法中,使用HttpURLConnection代替原来的URLConnection,原因是 URLConnection connect 之后返回 500 的话,是不能执行 getInputStream 方法的。需要使用getErrorStream方法,但是 URLConnection没有, HttpURLConnection 才有getErrorStream方法。 强制转换一下即可。
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
然后,需要判断 statusCode 是不是200, 否则应该使用getErrorStream()。
附参考:https://www.cnblogs.com/flyaway2013/p/14494092.html

Signed-off-by: huang_huang1 <liyanwong@sina.com>
pull/546/head
huang_huang1 2025-03-20 07:31:58 +00:00 committed by Gitee
parent 407f9f46d8
commit 3f117fc4c8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 25 additions and 4 deletions

View File

@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.MediaType;
import java.net.HttpURLConnection;
/**
* http
@ -72,12 +73,22 @@ public class HttpUtils
String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
log.info("sendGet - {}", urlNameString);
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
//获取状态码
int code = connection.getResponseCode();
System.out.println("ResponseCode="+code);
if (code == 200) {
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
} else {
// HttpURLConnection 才有getErrorStream方法
in = new BufferedReader(new InputStreamReader(connection.getErrorStream(), contentType));
}
String line;
while ((line = in.readLine()) != null)
{
@ -147,7 +158,7 @@ public class HttpUtils
{
log.info("sendPost - {}", url);
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
@ -158,7 +169,17 @@ public class HttpUtils
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
//获取状态码
int code = conn.getResponseCode();
System.out.println("ResponseCode="+code);
if (code == 200) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
} else {
// HttpURLConnection 才有getErrorStream方法
in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8));
}
String line;
while ((line = in.readLine()) != null)
{