第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

RestTemplate 調(diào)用外部 API 時(shí)被禁止(Cloudflare 服務(wù)器)

RestTemplate 調(diào)用外部 API 時(shí)被禁止(Cloudflare 服務(wù)器)

翻翻過去那場雪 2023-05-10 15:42:03
我在我的 REST 服務(wù)中使用一個(gè) REST API。當(dāng)我從 Chrome 或 Postman 調(diào)用 API 時(shí)一切正常,但當(dāng)我從我的應(yīng)用程序調(diào)用時(shí)返回 Forbbiden 響應(yīng)。PS:我正在使用 Java Spring Boot 項(xiàng)目。測試方法: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));}生產(chǎn):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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

當(dāng)我在標(biāo)頭中指定一些“用戶代理”時(shí),一切正常。


它似乎是對 CloudFlare 的限制: https://support.cloudflare.com/hc/en-us/articles/200170086-What-does-the-Browser-Integrity-Check-do-


我的應(yīng)用程序的用戶代理是我的 Java 版本 (Java/1.8.0_151)。如果您嘗試使用此用戶代理,您將收到來自 CloudFlare 的限制訪問消息。


卷曲:


curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan

響應(yīng):訪問被拒絕 | 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();

另一個(gè)解決方案:


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

    }


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

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

您的代碼實(shí)際上對我有用:


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/"}]}

也許你在一分鐘或一小時(shí)內(nèi)向服務(wù)發(fā)送了太多請求,他們已經(jīng)開始阻止你的 IP / 用戶代理或其他任何東西。


查看完整回答
反對 回復(fù) 2023-05-10
  • 2 回答
  • 0 關(guān)注
  • 330 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號