[英]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.