2 回答
TA贡献1784条经验 获得超9个赞
当我在标头中指定一些“用户代理”时,一切正常。
它似乎是对 CloudFlare 的限制: https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-
我的应用程序的用户代理是我的 Java 版本 (Java/1.8.0_151)。如果您尝试使用此用户代理,您将收到来自 CloudFlare 的限制访问消息。
卷曲:
curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan
响应:访问被拒绝 | swapi.co 使用 Cloudflare 来限制访问
此代码解决问题:
HttpHeaders headers = new HttpHeaders();
headers.add("user-agent", "Application");
HttpEntity<String> entity = new HttpEntity<>(headers);
String planetFound = restTemplate.exchange(findPlanetUri, HttpMethod.GET, entity, String.class).getBody();
另一个解决方案:
String planetFound = restTemplate.getForObject(findPlanetUri, String.class);
和
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("user-agent", "Application");
return execution.execute(request, body);
};
return restTemplateBuilder.additionalInterceptors(interceptor).build();
}
TA贡献1943条经验 获得超7个赞
您的代码实际上对我有用:
public static void main(String[] args) {
final String uri = "https://swapi.co/api/planets?search=Alderaan";
System.out.println(new RestTemplate().getForObject(uri, String.class));
}
输出是:
01:20:49.564 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https://swapi.co/api/planets?search=Alderaan
01:20:49.573 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
01:20:51.177 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
01:20:51.179 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json"
{"count":1,"next":null,"previous":null,"results":[{"name":"Alderaan","rotation_period":"24","orbital_period":"364","diameter":"12500","climate":"temperate","gravity":"1 standard","terrain":"grasslands, mountains","surface_water":"40","population":"2000000000","residents":["https://swapi.co/api/people/5/","https://swapi.co/api/people/68/","https://swapi.co/api/people/81/"],"films":["https://swapi.co/api/films/6/","https://swapi.co/api/films/1/"],"created":"2014-12-10T11:35:48.479000Z","edited":"2014-12-20T20:58:18.420000Z","url":"https://swapi.co/api/planets/2/"}]}
也许你在一分钟或一小时内向服务发送了太多请求,他们已经开始阻止你的 IP / 用户代理或其他任何东西。
添加回答
举报