包路径

org.springframework.web.reactive.function.client.WebClient

//实现类

代理方法:

@PostConstruct
public void initCommonWebClient() {
Function<HttpClient, HttpClient> mapper = client -> {
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(tcpClient ->
// 连接超时为60秒
tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1 * 60 * 1000)
.doOnConnected(conn -> conn
// 读取超时为60秒
.addHandlerLast(new ReadTimeoutHandler(60))
// 写入超时为60秒
.addHandlerLast(new WriteTimeoutHandler(60)))
.proxy(typeSpec -> typeSpec.type(ProxyProvider.Proxy.HTTP)
.address(new InetSocketAddress("127.0.0.1", 8888))));
return httpClient;
};
commonWebClient = webClientBuilder.clientConnector(new ReactorClientHttpConnector(reactorResourceFactory, mapper))
.codecs(clientCodecConfigurer ->{
clientCodecConfigurer.customCodecs().register(new Jackson2JsonDecoder());
clientCodecConfigurer.customCodecs().register(new Jackson2JsonEncoder());
clientCodecConfigurer.customCodecs().register(new Jaxb2XmlEncoder());
clientCodecConfigurer.customCodecs().register(new Jaxb2XmlDecoder());
})
.build();
}

其中标红的地方是实现代理方法,抓包后如下:

WebFlux中使用WebClient的时候抓取流量包_代理方法

本文中使用的是Fiddler5抓包的

其他参考:

​Spring WebClient 设置代理和https访问 - 简书 (jianshu.com)​