|
|
@@ -1,20 +1,14 @@
|
|
|
package com.njuzr.eaibackend.utils;
|
|
|
|
|
|
-import io.netty.handler.ssl.SslContextBuilder;
|
|
|
-import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
import org.springframework.web.reactive.function.client.WebClientResponseException;
|
|
|
import reactor.netty.http.client.HttpClient;
|
|
|
import reactor.netty.transport.ProxyProvider;
|
|
|
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.function.Consumer;
|
|
|
|
|
|
/**
|
|
|
* @author: Leonezhurui
|
|
|
@@ -23,51 +17,52 @@ import java.util.function.Consumer;
|
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
|
+@Component
|
|
|
public class WebClientUtil {
|
|
|
|
|
|
- @Value("${proxy.enable}")
|
|
|
- private Boolean enableProxy;
|
|
|
-
|
|
|
- @Value("${proxy.host}")
|
|
|
- private String proxyHost;
|
|
|
-
|
|
|
- @Value("${proxy.port}")
|
|
|
- private int proxyPort;
|
|
|
-
|
|
|
private final WebClient webClient;
|
|
|
|
|
|
- public WebClientUtil() {
|
|
|
- this.webClient = WebClient.builder().build();
|
|
|
+ public WebClientUtil(@Value("${proxy.enabled}") boolean proxyEnabled,
|
|
|
+ @Value("${proxy.host}") String proxyHost,
|
|
|
+ @Value("${proxy.port}") int proxyPort) {
|
|
|
+ HttpClient httpClient = HttpClient.create();
|
|
|
+ if (proxyEnabled) {
|
|
|
+ httpClient = httpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
|
|
|
+ .host(proxyHost)
|
|
|
+ .port(proxyPort));
|
|
|
+ }
|
|
|
+ this.webClient = WebClient.builder()
|
|
|
+ .clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
+ .build();
|
|
|
}
|
|
|
|
|
|
- // 构造函数,使用baseUrl初始化WebClient
|
|
|
- public WebClientUtil(String baseUrl) {
|
|
|
-// SslContextBuilder sslContextBuilder = SslContextBuilder
|
|
|
-// .forClient()
|
|
|
-// .trustManager(InsecureTrustManagerFactory.INSTANCE); // 信任所有证书
|
|
|
+
|
|
|
+// public WebClientUtil() {
|
|
|
+// this.webClient = WebClient.builder().build();
|
|
|
+// }
|
|
|
//
|
|
|
-// HttpClient httpClient = HttpClient.create()
|
|
|
-// .secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
|
|
|
+// public WebClientUtil(WebClient webClient) {
|
|
|
+// this.webClient = webClient;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public WebClientUtil(String url) {
|
|
|
// this.webClient = WebClient.builder()
|
|
|
-// .clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
-// .baseUrl(baseUrl)
|
|
|
+// .baseUrl(url)
|
|
|
// .build();
|
|
|
+// }
|
|
|
|
|
|
- if (enableProxy) {
|
|
|
- HttpClient httpClient = HttpClient.create()
|
|
|
- .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
|
|
|
- .host(proxyHost)
|
|
|
- .port(proxyPort));
|
|
|
- this.webClient = WebClient.builder()
|
|
|
- .clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
- .baseUrl(baseUrl)
|
|
|
- .build();
|
|
|
- } else {
|
|
|
- this.webClient = WebClient.builder()
|
|
|
- .baseUrl(baseUrl)
|
|
|
- .build();
|
|
|
- }
|
|
|
- }
|
|
|
+// public static WebClientUtil createWebClientWithProxy(String url, String proxyHost, int proxyPort) {
|
|
|
+// HttpClient httpClient = HttpClient.create()
|
|
|
+// .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
|
|
|
+// .host(proxyHost)
|
|
|
+// .port(proxyPort));
|
|
|
+//
|
|
|
+// WebClient webClient1 =WebClient.builder()
|
|
|
+// .clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
+// .baseUrl(url)
|
|
|
+// .build();
|
|
|
+// return new WebClientUtil(webClient1);
|
|
|
+// }
|
|
|
|
|
|
// GET请求方法
|
|
|
public <T> T get(String uri, Class<T> responseType) {
|