在Java 11 HTTP / 2客户端中读取时无法获得流1:达到EOF

时间:2018-10-29 08:55:42

标签: java httpclient http2 java-11

我目前正在使用Java 11(java.net.http模块)学习HTTP / 2客户端

我的目的是使用Java 11 HTTP / 2客户端向本地服务器发送POST JSON数据,但是我遇到了一些IOExceptionEOFException

例外

Exception in thread "main" java.io.IOException: Can't get stream 1: java.io.EOFException: EOF reached while reading
    at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:565)
    at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)
    at study/study.Http2ClientSample4.main(Http2ClientSample4.java:37)
Caused by: java.io.IOException: Can't get stream 1: java.io.EOFException: EOF reached while reading
    at java.net.http/jdk.internal.net.http.Exchange.lambda$checkForUpgradeAsync$12(Exchange.java:486)
    at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106)
    at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2235)
    at java.net.http/jdk.internal.net.http.Exchange.lambda$checkForUpgradeAsync$13(Exchange.java:474)
    at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106)
    at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2235)
    at java.net.http/jdk.internal.net.http.Exchange.checkForUpgradeAsync(Exchange.java:467)
    at java.net.http/jdk.internal.net.http.Exchange.lambda$wrapForUpgrade$10(Exchange.java:432)
    at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106)
    at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2235)
    at java.net.http/jdk.internal.net.http.Exchange.wrapForUpgrade(Exchange.java:432)
    at java.net.http/jdk.internal.net.http.Exchange.sendRequestBody(Exchange.java:399)
    at java.net.http/jdk.internal.net.http.Exchange.checkFor407(Exchange.java:354)
    at java.net.http/jdk.internal.net.http.Exchange.lambda$responseAsyncImpl0$7(Exchange.java:423)
    at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:930)
    at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:907)
    at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506)
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1705)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.EOFException: EOF reached while reading
    at java.net.http/jdk.internal.net.http.Http2Connection$Http2TubeSubscriber.onComplete(Http2Connection.java:1336)
    at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$ReadSubscription.signalCompletion(SocketTube.java:632)
    at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$InternalReadSubscription.read(SocketTube.java:833)
    at java.net.http/jdk.internal.net.http.SocketTube$SocketFlowTask.run(SocketTube.java:175)
    at java.net.http/jdk.internal.net.http.common.SequentialScheduler$SchedulableTask.run(SequentialScheduler.java:198)
    at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:271)
    at java.net.http/jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:224)
    at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$InternalReadSubscription.signalReadable(SocketTube.java:763)
    at java.net.http/jdk.internal.net.http.SocketTube$InternalReadPublisher$ReadEvent.signalEvent(SocketTube.java:941)
    at java.net.http/jdk.internal.net.http.SocketTube$SocketFlowEvent.handle(SocketTube.java:245)
    at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.handleEvent(HttpClientImpl.java:957)
    at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.lambda$run$3(HttpClientImpl.java:912)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at java.net.http/jdk.internal.net.http.HttpClientImpl$SelectorManager.run(HttpClientImpl.java:912)

我的代码

Path testPath = Paths.get( System.getProperty( "user.dir" ), "jsonSample.txt" );
byte[] testb = Files.readAllBytes( testPath );
System.out.println( new String( testb ) );

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
                .uri( URI.create( "http://localhost:8080" ) )
                .header("Content-Type", "application/json")
                .header( "Accept", "application/json" )
//              .header( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" )
                .POST( HttpRequest.BodyPublishers.ofFile( testPath ) )
                .build();

HttpResponse<?> response = client.send( request, HttpResponse.BodyHandlers.ofString());

System.out.println(response.statusCode());
System.out.println(response.body());

我在做什么错?

2 个答案:

答案 0 :(得分:0)

这可能是因为您的服务器不支持HTTP / 2。尝试将version(HttpClient.Version.HTTP_1_1)添加到HttpRequest

HttpRequest request = HttpRequest.newBuilder()
            .uri( URI.create( "http://localhost:8080" ) )
            .header("Content-Type", "application/json")
            .header( "Accept", "application/json" )
            .version(HttpClient.Version.HTTP_1_1)
            .POST( HttpRequest.BodyPublishers.ofFile( testPath ) )
            .build();

答案 1 :(得分:-1)

我遇到了同样的问题,但是我发现它是随机发生的,您可以重试,然后确定是否确实在您执行请求时出现了它。 如果没有,您可以使用try catch来捕获此异常,然后再次执行您的请求。