如何执行“OPTIONS” Java 11 HttpClient 中的 HTTP 方法请求
我需要用 java httpClient 替换 Apache http 客户端。
我用apache客户端进行了测试,效果很好:
HttpOptions httpOptions = new HttpOptions(uploadUrl);
try (CloseableHttpResponse responseOld = httpclient.execute(httpOptions)) {
assertEquals(200, responseOld.getStatusLine().getStatusCode());
assertEquals("POST,OPTIONS", responseOld.getFirstHeader("Access-Control-Allow-Methods").getValue());
}
但是当我将其更改为java客户端时,它不起作用。我使用此代码:
HttpClient javaClient = HttpClient.newBuilder().sslContext(initUnsecuredSSLContext()).build();
HttpRequest get = HttpRequest.newBuilder()
.uri(URI.create(uploadUrl))
.method("OPTIONS", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = javaClient.send(get, HttpResponse.BodyHandlers.ofString());
运行 java 版本“17.0.2”时测试无法完成
。
谢谢。
I need to replace Apache http client with java httpClient.
I have a test with an apache client that works well:
HttpOptions httpOptions = new HttpOptions(uploadUrl);
try (CloseableHttpResponse responseOld = httpclient.execute(httpOptions)) {
assertEquals(200, responseOld.getStatusLine().getStatusCode());
assertEquals("POST,OPTIONS", responseOld.getFirstHeader("Access-Control-Allow-Methods").getValue());
}
But when I change it to java client, it doesn't work. I use this code:
HttpClient javaClient = HttpClient.newBuilder().sslContext(initUnsecuredSSLContext()).build();
HttpRequest get = HttpRequest.newBuilder()
.uri(URI.create(uploadUrl))
.method("OPTIONS", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = javaClient.send(get, HttpResponse.BodyHandlers.ofString());
Test fails to complete when run
java version "17.0.2".
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切都通过响应的微小改变来解决
Everything was solved with a small change in response