[英]How do I prevent `java.IOException: HTTP1.1 header parser received no bytes`, when I need to wait for user to click a link in my app?
How do I wait for a my server to be updated?如何等待我的服务器更新? I am setting up my HttpServer
and can get a response from my own status I pass in to the createContext
handle
with my GET
method, but when I need to wait for the user (me) to click the Spotify auth link to redirect back to my server -我正在设置我的HttpServer
并且可以从我自己的状态中获得响应我使用我的GET
方法传递给createContext
handle
,但是当我需要等待用户(我)单击 Spotify auth 链接以重定向回我的服务器 -
java.util.concurrent.ExecutionException: java.io.IOException: HTTP/1.1 header parser received no bytes
If I manually set the header String my GET
will return the body fine.如果我手动设置 header 字符串,我的GET
将返回主体。 I've tried serverSocket
and CompleteableFuture async(request, HttpResponse.BodyHandlers.ofString()
( from https://openjdk.java.net/groups/net/httpclient/recipes.html#asynchronousGet ) and still get the above exception.我已经尝试过serverSocket
和CompleteableFuture async(request, HttpResponse.BodyHandlers.ofString()
(来自https://openjdk.java.net/groups/net/httpclient/recipes.html#asynchronousGet )并且仍然得到上述异常。
In Chrome when I open http://localhost:8080 the Spotify code is there.在 Chrome 中,当我打开http://localhost:8080时,Spotify 代码就在那里。
I'm thinking I need to loop the client.send(request, HttpResponse.BodyHandleers.ofString()
until the server status code is updated, or set a wait time?我想我需要循环client.send(request, HttpResponse.BodyHandleers.ofString()
直到服务器状态代码更新,或者设置等待时间?
Here is the time I've been testing.这是我一直在测试的时间。
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);
}
I am following a Hyperskill.org project.我正在关注 Hyperskill.org 项目。 This is how the code should behave when this stage is complete.这就是该阶段完成后代码的行为方式。
> 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!---
I have just passed this stage and was having the same error, the problem is the errors that can lead to this error message are not really that specific as it seems.我刚刚通过了这个阶段并且遇到了同样的错误,问题是可能导致此错误消息的错误并不像看起来那么具体。 For me it was because of null values coming from "exchange.getRequestURI().getQuery()" i was calling.startsWith("code'") over this potential null value resulting in never sending a response and then the "HTTP/1.1 header parser received no bytes".对我来说,这是因为 null 值来自“exchange.getRequestURI().getQuery()”我在这个潜在的 null 值上调用.startsWith(“code'”) 导致永远不会发送响应,然后是“HTTP/1.1 header 解析器未收到任何字节”。 You might be expecting the program to crash and get a different error message at console, but the server run in a different thread from the main, so the server thread crashes not the main.您可能期望程序崩溃并在控制台上收到不同的错误消息,但是服务器在与主线程不同的线程中运行,因此服务器线程不是主线程崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.