java 10 httpclient孵化器GET请求在node.js服务器

时间:2018-05-14 20:16:54

标签: java httpclient java-10

我一直在试验Java 9/10孵化器中的HttpClient内容,并且有以下简单的代码(实际上是从项目主页上偷来的!):

URI uri = URI.create("http://192.168.1.102:8080/");

HttpRequest getRequest = HttpRequest.newBuilder()
    .uri(uri)
    .GET()
    .build();

HttpResponse<String> response = client.send(getRequest,
    HttpResponse.BodyHandler.asString());
System.out.println("response to get: " + response.body());

如果它指向不是localhost的URL,我发现它工作正常,但如果我要求localhost(无论是名称&#34; localhost&#34;,172.0.0.1),我都会失败,或者通过本地主机的实际IP地址)。错误很奇怪,整个堆栈跟踪都没有提到我的任何代码。

WARNING: Using incubator modules: jdk.incubator.httpclient
Exception in thread "main" java.io.EOFException: EOF reached while reading
    at jdk.incubator.httpclient/jdk.incubator.http.Http1AsyncReceiver$Http1TubeSubscriber.onComplete(Http1AsyncReceiver.java:507)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$InternalReadPublisher$ReadSubscription.signalCompletion(SocketTube.java:551)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$InternalReadPublisher$InternalReadSubscription.read(SocketTube.java:728)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$SocketFlowTask.run(SocketTube.java:171)
    at jdk.incubator.httpclient/jdk.incubator.http.internal.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:198)
    at jdk.incubator.httpclient/jdk.incubator.http.internal.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:271)
    at jdk.incubator.httpclient/jdk.incubator.http.internal.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:224)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$InternalReadPublisher$InternalReadSubscription.signalReadable(SocketTube.java:675)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$InternalReadPublisher$ReadEvent.signalEvent(SocketTube.java:829)
    at jdk.incubator.httpclient/jdk.incubator.http.SocketTube$SocketFlowEvent.handle(SocketTube.java:243)
    at jdk.incubator.httpclient/jdk.incubator.http.HttpClientImpl$SelectorManager.handleEvent(HttpClientImpl.java:769)
    at jdk.incubator.httpclient/jdk.incubator.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:731)

有一台服务器在本地运行,我可以使用来自网络浏览器的简单请求连接到它。

有什么想法?

[编辑]我发现,我相信,这个项目的邮件列表。它混淆了&#34;&#34; (这完全欺骗了我!)但显示为:net dash dev at openjdk dot java dot net我也会在那里发帖,看看他们是否有任何意见。

[编辑2]我很确定这与localhost(每个原始标题)无关,但是与node.js / express协议协商(这是服务器我&m; m)使用,因为它很容易实验)。节点偶尔(例如,最后一行文本不是LF终止)似乎报告错误的内容长度,但这不是问题,因为失败仍然以正确的长度发生。我认为尝试将连接升级到HTTP / 2.0可能是一个错误,但还不知道......

[编辑3]在浪费了我太多的生命实验之后,我非常确定node.js 8.11.1中的某些东西(并表达了4.13.4和body-parser) 1.15.1)处理升级到导致问题的HTTP 2.0的请求。但我不知道是什么。我放弃了,并将继续使用不同服务器的httpClient学习过程。

2 个答案:

答案 0 :(得分:1)

更新。我终于得到了使用http 2.0支持构建的curl,并且责备完全在node / express上。当此服务器看到升级请求(节点8.something)时,它无法创建任何输出。因此,客户端正确失败并出现EOF错误。

作为旁注,node / express还将内容长度标题设置为#34;关闭一个&#34;有时(并非总是!?)

答案 1 :(得分:1)

试试这个

HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI("http://localhost:3000"))
                    .POST(BodyPublisher.fromString("hello"))
                    .version(Version.HTTP_1_1).build();