包路径
org.springframework.web.reactive.function.client.WebClient //实现类 使用WebClient.Builder进行build
代理方法:
@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(); }
其中标红的地方是实现代理方法,抓包后如下:
本文中使用的是Fiddler5抓包的
其他参考:
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。