如何在Reactor Netty客户端上使用简单的HTTP代理?

时间:2018-09-06 07:08:16

标签: http netty http-proxy reactor-netty

我正在尝试使用一个简单的HTTP代理从外部缓存响应。我尝试了以下代码段:

public static void main(String[] args) {
  HttpClient httpClient = HttpClient.builder().options(httpOptions -> {
    httpOptions.proxy(proxyOptions -> proxyOptions
        .type(Proxy.HTTP)
        .address(InetSocketAddress.createUnresolved("localhost", 7000))
    );
  }).build();
  String url = "http://httpbin.org/get";
  HttpClientResponse response = httpClient.get(url).block();
  System.out.println(response == null ? "null" : response.status().code());
}

...但是结果是客户端以CONNECT命令开头,创建了TCP隧道。 当我使用curl时,我将执行以下操作:

curl "http://httpbin.org/get" -x "localhost:7000"

...,它只是针对代理发出常规HTTP请求。

我的问题:我如何使用Raector-Netty针对代理发出常规请求(不是基于CONNECT的请求)?

1 个答案:

答案 0 :(得分:0)

    ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options ->
            options.httpProxy(addressSpec -> {
                return addressSpec.host("http://proxy_server").port(proxy_port); }));

    webClient = WebClient.builder()
                         .clientConnector(connector)
                         .baseUrl(baseUrl)
                         .build();