其他分享
首页 > 其他分享> > WebFlux中使用WebClient的时候抓取流量包

WebFlux中使用WebClient的时候抓取流量包

作者:互联网

包路径

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)

标签:register,WebFlux,抓取,clientCodecConfigurer,60,new,customCodecs,WebClient
来源: https://www.cnblogs.com/passedbylove/p/16101891.html