如何防止`java.IOException:HTTP1.1头解析器没有接收到字节`,当我需要等待用户点击我的应用程序中的



如何等待服务器更新?我正在设置我的HttpServer,可以从我自己的状态中获得响应,我用GET方法将其传递给createContexthandle,但当我需要等待用户(我(点击Spotify auth链接重定向回我的服务器-时

java.util.concurrent.ExecutionException: java.io.IOException: HTTP/1.1 header parser received no bytes

如果我手动设置标题String,我的GET将返回正文。我试过serverSocketCompleteableFuture async(request, HttpResponse.BodyHandlers.ofString()(来自https://openjdk.java.net/groups/net/httpclient/recipes.html#asynchronousGet(,并且仍然得到上述异常。

打开时使用Chromehttp://localhost:8080Spotify代码就在那里。

我想我需要循环client.send(request, HttpResponse.BodyHandleers.ofString(),直到服务器状态代码更新,或者设置等待时间?

这是我测试的时间。

public static void startHttpServer() {
try {
server = HttpServer.create();
server.bind(new InetSocketAddress(8080), 0);
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
String query = /*"hey buddy, this is a Java server, wouldn't you know"; */exchange.getRequestURI().getQuery();
exchange.sendResponseHeaders(200, query.length());
exchange.getResponseBody().write(query.getBytes());
exchange.getResponseBody().close();
}
});
server.start();
System.out.println("*** Started server ***");
System.out.println("Use this link to request the access code:");
System.out.println("https://accounts.spotify.com/authorize?client_id=6edb9b1ac21042abacc6daaf0fbc4c4d&redirect_uri=http://localhost:8080&response_type=code");
} catch (IOException e) {
e.printStackTrace();
}
}
/*public static void getResponse() { // get the response from the server?!
try {
HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080"))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
server.stop(1);
}
}*/
public static CompletableFuture<String> get() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080"))
.GET()
.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
}

我正在关注Hyperskill.org项目。这就是这个阶段完成后代码的行为方式。

> new
Please, provide access for application.
> auth
use this link to request the access code:
https://accounts.spotify.com/authorize?client_id=a19ee7dbfda443b2a8150c9101bfd645&redirect_uri=http://localhost:8080&response_type=code
waiting for code...
code received
making http request for access_token...
response:
{"access_token":"BQBSZ0CA3KR0cf0LxmiNK_E87ZqnkJKDD89VOWAZ9f0QXJcsCiHtl5Om-EVhkIfwt1AZs5WeXgfEF69e4JxL3YX6IIW9zl9WegTmgLkb4xLXWwhryty488CLoL2SM9VIY6HaHgxYxdmRFGWSzrgH3dEqcvPoLpd26D8Y","token_type":"Bearer","expires_in":3600,"refresh_token":"AQCSmdQsvsvpneadsdq1brfKlbEWleTE3nprDwPbZgNSge5dVe_svYBG-RG-_PxIGxVvA7gSnehFJjDRAczLDbbdWPjW1yUq2gtKbbNrCQVAH5ZBtY8wAYskmOIW7zn3IEiBzg","scope":""}
---SUCCESS---
> new
---NEW RELEASES---
Mountains [Sia, Diplo, Labrinth]
Runaway [Lil Peep]
The Greatest Show [Panic! At The Disco]
All Out Life [Slipknot]
> exit
---GOODBYE!---

我刚刚通过了这个阶段,遇到了同样的错误,问题是可能导致这个错误消息的错误并不像看上去那么具体。对我来说,这是因为来自";exchange.getRequestURI((.getQuery(("我在这个潜在的null值上调用.startsWith("code"(,导致永远不会发送响应;HTTP/1.1报头解析器没有接收到字节";。您可能希望程序崩溃,并在控制台上得到不同的错误消息,但服务器运行在与主线程不同的线程中,因此服务器线程崩溃而不是主线程。

最新更新