https代理

gkl3eglg  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(207)

我想用JDK11 HttpClient 通过https代理执行请求。我尝试了以下代码来创建客户端:

HttpClient client = HttpClient
    .newBuilder()
    .proxy(ProxySelector.of(new InetSocketAddress("my.proxy.host",8443)))
    .version(Version.HTTP_1_1)
    .build();

以及生成请求的代码:

String auth = new String(Base64.getEncoder().encode("username:password".getBytes()));
var request =
HttpRequest
    .newBuilder(new URI("http://my.internal.host/path"))
    .POST(BodyPublishers.ofString("{\"foo\":\"bar\"}"))
    .header("Proxy-Authorization", "Basic " + auth)
    .header("Content-Type", "application/json")
    .build();

但它失败了 java.io.IOException: HTTP/1.1 header parser received no bytes .
等价物 curl 我想表演的是:

curl -XPOST --proxy 'https://username:password@http://my.proxy.host:8443/path' \
  -d '{"foo":"bar"}' \
  -h 'Content-Type: application/json' \
  http://my.internal.host/path

这就行了。有什么建议吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题