Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RestTemplate调用外部API时禁止(Cloudflare server)_Java_Rest_Spring Boot_Integration Testing - Fatal编程技术网

Java RestTemplate调用外部API时禁止(Cloudflare server)

Java RestTemplate调用外部API时禁止(Cloudflare server),java,rest,spring-boot,integration-testing,Java,Rest,Spring Boot,Integration Testing,我在REST服务中使用了一个RESTAPI。当我从Chrome或Postman调用API时,一切都正常,但当我从应用程序调用API时,会返回Forbbiden响应 PS:我正在使用一个JavaSpring启动项目 试验方法: public static void main(String[] args) { final String uri = "https://swapi.co/api/planets?search=Alderaan"; System.out.println(ne

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

PS:我正在使用一个JavaSpring启动项目

试验方法:

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
curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan
 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();
产生:

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
curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan
 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();

外部API:

您的代码实际上适用于我:

publicstaticvoidmain(字符串[]args){
最终字符串uri=”https://swapi.co/api/planets?search=Alderaan";
System.out.println(新的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/用户代理或诸如此类的操作。

当我在标题中指定某个“用户代理”时,一切都正常

这可能是对CloudFlare的限制:

我的应用程序的用户代理是我的Java版本(Java/1.8.0_151)。如果尝试此用户代理,您将从CloudFlare收到一条限制访问的消息

卷曲:

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
curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan
 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();
响应:拒绝访问| swapi.co使用Cloudflare限制访问

此代码解决了问题:

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
curl -H "User-Agent: Java/1.8.0_151" https://swapi.co/api/planets/?search=Alderaan
 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();


与您使用的代码相同:20:58:01.436[main]DEBUG org.springframework.web.client.restemplate-HTTP GET 20:58:01.461[main]DEBUG org.springframework.web.client.restemplate-Accept=[text/plain,application/json,application/*+json,/]20:58:02.577[main]DEBUG org.springframework.web.client.restemplate-Response 403我将用您的代码编辑我的问题,使之更简单。Tks@VictorSoares关于这一点,您是否尝试过联系“swapi.co”服务背后的支持人员(或开发人员)?也许他们有某种DDOS防护系统,而你受到了影响。另外,您是否使用任何类型的代理来访问“swapi.co”服务?啊!我看到了您自己对问题的答案:正如我所怀疑的,您被DDOS保护系统阻止,该系统有简单的规则来过滤某些用户代理发送的请求。VladimirDid您是否也使用Spring Security?