From 4955cdc73d9beb5733aa2c0a578c14798394fa61 Mon Sep 17 00:00:00 2001 From: xuhy <3313886187@qq.com> Date: 星期二, 23 九月 2025 18:06:45 +0800 Subject: [PATCH] AI对接 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java | 147 +++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 142 insertions(+), 5 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 6d9fa34..930c605 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 @@ -5,18 +5,34 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; -import java.net.ConnectException; -import java.net.SocketTimeoutException; -import java.net.URL; -import java.net.URLConnection; +import java.net.*; import java.nio.charset.StandardCharsets; import java.security.cert.X509Certificate; +import java.util.Iterator; +import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; + +import com.ruoyi.common.exception.ServiceException; +import org.apache.http.Consts; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ruoyi.common.constant.Constants; @@ -116,7 +132,128 @@ } return result.toString(); } + /** + * 向指定 URL 发送GET方法的请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @param contentType 编码类型 + * @return 所代表远程资源的响应结果 + */ + public static String sendGet(String url, String param, String contentType,String token) + { + 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.setRequestProperty("Authorization","bearer "+ token); + 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(); + } + /*** + * get请求(带参数) + * @param url + * @return String + */ + public static String getReq(String url, Map<String, String> params,String token) { + String result = null; + try { + URIBuilder uriBuilder = new URIBuilder(url); + Iterator maplist = params.entrySet().iterator(); + while (maplist.hasNext()) { + Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next(); + uriBuilder.addParameter(map.getKey(), map.getValue()); + } + CloseableHttpClient client = HttpClientBuilder.create().build(); + HttpGet get = new HttpGet(uriBuilder.build()); + get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); + get.addHeader("Authorization","bearer "+ token); + HttpResponse response = client.execute(get); + result = EntityUtils.toString(response.getEntity(), "UTF-8"); + } catch (URISyntaxException e) { + e.printStackTrace(); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } + /*** + * get请求(带参数) + * @param url + * @return String + */ + public static String postReq(String url, Map<String, String> params,String token) { + String result = null; + try { + URIBuilder uriBuilder = new URIBuilder(url); + Iterator maplist = params.entrySet().iterator(); + while (maplist.hasNext()) { + Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next(); + uriBuilder.addParameter(map.getKey(), map.getValue()); + } + CloseableHttpClient client = HttpClientBuilder.create().build(); + HttpPost post = new HttpPost(uriBuilder.build()); + post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); + post.addHeader("Authorization",token); + HttpResponse response = client.execute(post); + result = EntityUtils.toString(response.getEntity(), "UTF-8"); + } catch (URISyntaxException e) { + e.printStackTrace(); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } /** * 向指定 URL 发送POST方法的请求 * @@ -211,7 +348,7 @@ conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("contentType", "utf-8"); - conn.setRequestProperty("Authorization","bearer "+ token); + conn.setRequestProperty("Authorization",token); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); -- Gitblit v1.7.1