0

サーバーが更新されるのを待つにはどうすればよいですか? 私は自分HttpServerをセットアップしていて、自分のステータスから応答を取得できます。createContext handle自分のGETメソッドでに渡しますが、ユーザー (私) が Spotify auth リンクをクリックしてサーバーにリダイレクトするのを待つ必要がある場合 -

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

ヘッダー文字列を手動で設定するGETと、本文が正常に返されます。私は(https://openjdk.java.net/groups/net/httpclient/recipes.html#asynchronousGetserverSocketから)試してみましたが、それでも上記の例外が発生します。CompleteableFuture async(request, HttpResponse.BodyHandlers.ofString()

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!---
4

1 に答える 1

1

この段階を通過したばかりで、同じエラーが発生していました。問題は、このエラー メッセージにつながる可能性のあるエラーが、見た目ほど具体的ではないことです。私にとっては、「exchange.getRequestURI().getQuery()」からの null 値が原因でした。ヘッダーパーサーはバイトを受け取りませんでした」. プログラムがクラッシュしてコンソールに別のエラー メッセージが表示されることを期待しているかもしれませんが、サーバーはメインとは別のスレッドで実行されているため、サーバー スレッドはメインではなくクラッシュします。

于 2020-09-30T05:44:06.300 に答える