亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

RestTemplate 調用外部 API 時被禁止(Cloudflare 服務器)

RestTemplate 調用外部 API 時被禁止(Cloudflare 服務器)

翻翻過去那場雪 2023-05-10 15:42:03
我在我的 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 FORBIDDENException in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden外部 API: https: //swapi.co/documentation
查看完整描述

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

    }


查看完整回答
反對 回復 2023-05-10
?
楊__羊羊

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 / 用戶代理或其他任何東西。


查看完整回答
反對 回復 2023-05-10
  • 2 回答
  • 0 關注
  • 339 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號