|
|
@@ -3,13 +3,17 @@ 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.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
|
|
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
|
|
|
@@ -38,7 +42,13 @@ public class WebClientUtil {
|
|
|
// .baseUrl(baseUrl)
|
|
|
// .build();
|
|
|
|
|
|
+ HttpClient httpClient = HttpClient.create()
|
|
|
+ .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
|
|
|
+ .host("127.0.0.1")
|
|
|
+ .port(7890));
|
|
|
+
|
|
|
this.webClient = WebClient.builder()
|
|
|
+ .clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
.baseUrl(baseUrl)
|
|
|
.build();
|
|
|
}
|
|
|
@@ -102,5 +112,19 @@ public class WebClientUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public <T, R> T postWithToken(String uri, R request, Class<T> responseType, String key) {
|
|
|
+ try {
|
|
|
+ return this.webClient.post()
|
|
|
+ .uri(uri)
|
|
|
+ .header("Authorization", "Bearer " + key)
|
|
|
+ .bodyValue(request)
|
|
|
+ .retrieve()
|
|
|
+ .bodyToMono(responseType)
|
|
|
+ .block(); // 转换为阻塞调用
|
|
|
+ } catch (WebClientResponseException e) {
|
|
|
+ log.error("WebClient发送POST请求失败:" + e.getMessage());
|
|
|
+ throw new RuntimeException("Failed to post data: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|