问题描述
我想使用JDK 11 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
那行得通。有什么建议吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)