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?
"jsonSample.txt"
includes? – IntercalateURI.create("http://www.example.com/")
, what is the local service actually supposed to do and could it be responsible for the IO exception? – Intercalate