RestTemplate 调用外部 API 时被禁止(Cloudflare 服务器)

我在我的 REST 服务中使用一个 REST API。当我从 Chrome 或 Postman 调用 API 时一切正常,但当我从我的应用程序调用时返回 Forbbiden 响应。


PS:我正在使用 Java Spring Boot 项目。


测试方法:


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));

}

生产:


20:58:01.436 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET swapi.co/api/planets?search=Alderaan 

20:58:01.461 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]

20:58:02.577 [main] DEBUG org.springframework.web.client.RestTemplate - Response 403 FORBIDDEN

Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden

外部 API: https: //swapi.co/documentation


翻翻过去那场雪
浏览 243回答 2
2回答

千万里不及你

当我在标头中指定一些“用户代理”时,一切正常。它似乎是对 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 来限制访问此代码解决问题:&nbsp;HttpHeaders headers = new HttpHeaders();&nbsp;headers.add("user-agent", "Application");&nbsp;HttpEntity<String> entity = new HttpEntity<>(headers);&nbsp;String planetFound = restTemplate.exchange(findPlanetUri, HttpMethod.GET, entity, String.class).getBody();另一个解决方案:&nbsp;String planetFound = restTemplate.getForObject(findPlanetUri, String.class);和&nbsp; &nbsp;@Bean&nbsp; &nbsp; public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {&nbsp; &nbsp; &nbsp; &nbsp; ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.getHeaders().add("user-agent", "Application");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return execution.execute(request, body);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; return restTemplateBuilder.additionalInterceptors(interceptor).build();&nbsp; &nbsp; }

杨__羊羊

您的代码实际上对我有用:public static void main(String[] args) {&nbsp; &nbsp; final String uri = "https://swapi.co/api/planets?search=Alderaan";&nbsp; &nbsp; 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=Alderaan01: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 OK01: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 / 用户代理或其他任何东西。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java