Can't get Stream 1: EOF reached while reading in Java 11 HTTP/2 Client
Asked Answered
M

2

7

I'm currently learning HTTP/2 Client in Java 11(java.net.http module)

My purpose is send a POST JSON data to local server using Java 11 HTTP/2 Client, but I occurred some IOException, EOFException.

Exceptions

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)

My Code

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());

What am I doing wrong?

Mossback answered 29/10, 2018 at 8:55 Comment(4)
what does your file "jsonSample.txt"includes?Intercalate
that file has { "firstName": "John", "lastName": "Doe", "age": 21 }Ceasefire
The above just works fine for me, if I POST it to a sample web service using URI.create("http://www.example.com/"), what is the local service actually supposed to do and could it be responsible for the IO exception?Intercalate
Oh.. thanks for checking my code. I will check my local erver, and then comment on. Thank you!Ceasefire
D
12

This could be because your server does not support HTTP/2. Try adding version(HttpClient.Version.HTTP_1_1) to the 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();
Dorsey answered 15/8, 2019 at 14:4 Comment(0)
B
0

I have met the same question, but I find it is occur in random, you can try again and find whether it is occur certainly when you do your request. If not, you can use try catch to catch this exception and do your request again.

Bondage answered 29/1, 2019 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.