From 3f117fc4c84af86f9a14253539516f261d18e3fd Mon Sep 17 00:00:00 2001 From: huang_huang1 Date: Thu, 20 Mar 2025 07:31:58 +0000 Subject: [PATCH] =?UTF-8?q?update=20ruoyi-common/src/main/java/com/ruoyi/c?= =?UTF-8?q?ommon/utils/http/HttpUtils.java.=20public=20static=20String=20s?= =?UTF-8?q?endGet(String=20url,=20String=20param,=20String=20contentType)?= =?UTF-8?q?=E5=92=8C=20public=20static=20String=20sendPost(String=20url,?= =?UTF-8?q?=20String=20param,=20String=20contentType)=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E4=BD=BF=E7=94=A8HttpURLConnection=E4=BB=A3?= =?UTF-8?q?=E6=9B=BF=E5=8E=9F=E6=9D=A5=E7=9A=84URLConnection=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0=E6=98=AF=20URLConnection=20connect=20?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E8=BF=94=E5=9B=9E=20500=20=E7=9A=84=E8=AF=9D?= =?UTF-8?q?=EF=BC=8C=E6=98=AF=E4=B8=8D=E8=83=BD=E6=89=A7=E8=A1=8C=20getInp?= =?UTF-8?q?utStream=20=E6=96=B9=E6=B3=95=E7=9A=84=E3=80=82=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E4=BD=BF=E7=94=A8getErrorStream=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=20URLConnection=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=EF=BC=8C=20HttpURLConnection=20=E6=89=8D=E6=9C=89getErrorStrea?= =?UTF-8?q?m=E6=96=B9=E6=B3=95=E3=80=82=20=E5=BC=BA=E5=88=B6=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E4=B8=80=E4=B8=8B=E5=8D=B3=E5=8F=AF=E3=80=82=20HttpUR?= =?UTF-8?q?LConnection=20urlConnection=20=3D=20(HttpURLConnection)=20url.o?= =?UTF-8?q?penConnection();=20=E7=84=B6=E5=90=8E=EF=BC=8C=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=88=A4=E6=96=AD=20statusCode=20=E6=98=AF=E4=B8=8D?= =?UTF-8?q?=E6=98=AF200=EF=BC=8C=20=E5=90=A6=E5=88=99=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8getErrorStream()=E3=80=82=20=E9=99=84?= =?UTF-8?q?=E5=8F=82=E8=80=83=EF=BC=9Ahttps://www.cnblogs.com/flyaway2013/?= =?UTF-8?q?p/14494092.html?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huang_huang1 --- .../ruoyi/common/utils/http/HttpUtils.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java index d50578915..5dd52496f 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java @@ -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) {