包路径

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抓包的

其他参考:

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

posted on 2022-04-05 12:22  你不知道的浪漫  阅读(120)  评论(0编辑  收藏  举报