gpt4 book ai didi

java - 无法获取流 1 : EOF reached while reading in Java 11 HTTP/2 Client

转载 作者:行者123 更新时间:2023-11-29 09:26:56 26 4
gpt4 key购买 nike

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

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

异常(exception)情况

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

我做错了什么?

最佳答案

这可能是因为您的服务器不支持 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();

关于java - 无法获取流 1 : EOF reached while reading in Java 11 HTTP/2 Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53041856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com