[英]Caused by: java.io.IOException: HTTP/1.1 header parser received no bytes
[英]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?
如何等待我的服務器更新? 我正在設置我的HttpServer
並且可以從我自己的狀態中獲得響應我使用我的GET
方法傳遞給createContext
handle
,但是當我需要等待用戶(我)單擊 Spotify auth 鏈接以重定向回我的服務器 -
java.util.concurrent.ExecutionException: java.io.IOException: HTTP/1.1 header parser received no bytes
如果我手動設置 header 字符串,我的GET
將返回主體。 我已經嘗試過serverSocket
和CompleteableFuture async(request, HttpResponse.BodyHandlers.ofString()
(來自https://openjdk.java.net/groups/net/httpclient/recipes.html#asynchronousGet )並且仍然得到上述異常。
在 Chrome 中,當我打開http://localhost:8080時,Spotify 代碼就在那里。
我想我需要循環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!---
我剛剛通過了這個階段並且遇到了同樣的錯誤,問題是可能導致此錯誤消息的錯誤並不像看起來那么具體。 對我來說,這是因為 null 值來自“exchange.getRequestURI().getQuery()”我在這個潛在的 null 值上調用.startsWith(“code'”) 導致永遠不會發送響應,然后是“HTTP/1.1 header 解析器未收到任何字節”。 您可能期望程序崩潰並在控制台上收到不同的錯誤消息,但是服務器在與主線程不同的線程中運行,因此服務器線程不是主線程崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.