当我的应用程序需要等待用户点击链接时,如何防止 "java.IOException.HTTP1.1 header parser received no bytes"?当我需要等待用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我的应用程序需要等待用户点击链接时,如何防止 "java.IOException.HTTP1.1 header parser received no bytes"?当我需要等待用相关的知识,希望对你有一定的参考价值。

如何等待服务器更新?我正在设置我的 HttpServer 并能从我自己的状态中得到回应,我传入的 createContext handle 与我 GET 方法,但当我需要等待用户(我)点击Spotify auth链接重定向回我的服务器时--。

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

如果我手动设置了标题字符串,我的 GET 将返回身体罚款。我试过 serverSocketCompleteableFuture async(request, HttpResponse.BodyHandlers.ofString() ( 从 https:/openjdk.java.netgroupsnethttpclientrecipes.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!---

以上是关于当我的应用程序需要等待用户点击链接时,如何防止 "java.IOException.HTTP1.1 header parser received no bytes"?当我需要等待用的主要内容,如果未能解决你的问题,请参考以下文章

单击窗口时如何防止c ++程序停止?

如何在保持程序化行选择的同时防止用户点击行选择?

在android webview应用程序的页面上单击链接时如何防止显示加载微调器?

点击返回按钮时防止重新提交表单

激活用户Django 2.1

检测 PowerModeChange 并等待执行