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 * @Date: 2024/2/25 - 16:52 * @Package: EAI-Backend */ @Slf4j public class WebClientUtil { private final WebClient webClient; public WebClientUtil() { this.webClient = WebClient.builder().build(); } // 构造函数,使用baseUrl初始化WebClient public WebClientUtil(String baseUrl) { // SslContextBuilder sslContextBuilder = SslContextBuilder // .forClient() // .trustManager(InsecureTrustManagerFactory.INSTANCE); // 信任所有证书 // // HttpClient httpClient = HttpClient.create() // .secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder)); // this.webClient = WebClient.builder() // .clientConnector(new ReactorClientHttpConnector(httpClient)) // .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(); } // GET请求方法 public T get(String uri, Class responseType) { try { return this.webClient.get() .uri(uri) .retrieve() .bodyToMono(responseType) .block(); // 转换为阻塞调用 } catch (WebClientResponseException e) { log.error("WebClient发送GET请求失败:" + e.getMessage()); throw new RuntimeException("Failed to get response: " + e.getMessage(), e); } } // POST请求方法 public T post(String uri, R request, Class responseType) { try { return this.webClient.post() .uri(uri) .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); } } // PUT请求方法 public T put(String uri, R request, Class responseType) { try { return this.webClient.put() .uri(uri) .bodyValue(request) .retrieve() .bodyToMono(responseType) .block(); // 转换为阻塞调用 } catch (WebClientResponseException e) { log.error("WebClient发送PUT请求失败:" + e.getMessage()); throw new RuntimeException("Failed to put data: " + e.getMessage(), e); } } // DELETE请求方法 public T delete(String uri, Class responseType) { try { return this.webClient.delete() .uri(uri) .retrieve() .bodyToMono(responseType) .block(); // 转换为阻塞调用 } catch (WebClientResponseException e) { log.error("WebClient发送DELETE请求失败:" + e.getMessage()); throw new RuntimeException("Failed to delete resource: " + e.getMessage(), e); } } public T postWithToken(String uri, R request, Class 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); } } }