简体   繁体   English

为什么我只在使用 Java HTTP 客户端时得到 HTTP 响应状态码 451?

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

I want to make a GET-request, which works fine with OkHttpClient, my webbrowsers and postman.我想发出一个 GET 请求,它适用于 OkHttpClient、我的网络浏览器和 postman。 But when I use the Java HTTP Client, I receive the Response Code 451. This is my code:但是当我使用 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" is what is printed. “451”是打印的内容。 Why?为什么?

Adding the "User-Agent"-Header solved my problem.添加“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.

相关问题 当响应具有HTTP错误状态代码时,为什么会出现“只允许一个连接接收订户”? - Why do I get `Only one connection receive subscriber allowed` when the response has an HTTP error status code? 如何从微服务获取http响应状态码-zuul [java] - How to get http response status code from microservice - zuul [java] 如何在java中获得对http的响应? - How do I get a response to a http in java? 抛出 RestClientException 时如何检索 HTTP 状态代码和响应正文? - How do I retrieve HTTP status code and response body when an RestClientException is thrown? 为什么在使用“ apache httpclient”和“ httpurlconnection”时http状态码不同? - Why http status code is different when using “apache httpclient” and “httpurlconnection”? Java-当我尝试获取源代码服务器时返回的HTTP响应代码:400 - Java - when I tried to get the source code server returned HTTP response code: 400 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code 如何在Soap客户端中获取Http响应代码? - How to get an Http response code in a soap client? Java中的HTTP响应代码 - HTTP response code in Java 在使用Apache的HTTP客户端时,将HTTP响应作为字符串的建议方法是什么? - What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM