簡體   English   中英

為什么我只在使用 Java HTTP 客戶端時得到 HTTP 響應狀態碼 451?

[英]Why do I get HTTP Response Status Code 451 only when using the Java HTTP Client?

我想發出一個 GET 請求,它適用於 OkHttpClient、我的網絡瀏覽器和 postman。 但是當我使用 Java HTTP 客戶端時,我收到響應代碼 451。這是我的代碼:

HttpClient client = HttpClient.newHttpClient();
             HttpRequest request = HttpRequest.newBuilder()
                   .uri(URI.create("*Some URL*"))
                   .build();

             
            HttpResponse<String> response=null;
            try {
                response = client.send(request, BodyHandlers.ofString());
                System.out.println(response.statusCode());
                System.out.println(response.body());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }System.out.println(response.body());
             

“451”是打印的內容。 為什么?

添加“User-Agent”-Header 解決了我的問題。

HttpRequest request = HttpRequest.newBuilder()
                       .uri(URI.create("*URL*"))
                       .header("User-Agent", "Mozilla/5.0")
                       .build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM