java 收到 403 禁止错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38857598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting 403 forbidden error
提问by sr7
Getting 403 forbidden for below code, though "https://jsonplaceholder.typicode.com/posts/1" works in postman
尽管“ https://jsonplaceholder.typicode.com/posts/1”适用于邮递员,但以下代码禁止使用 403
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
RestTemplate rt = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
String url = "https://jsonplaceholder.typicode.com/posts/1";
ResponseEntity<String> res = rt.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println(res);
}
}
Error:
错误:
23:28:21.447 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://jsonplaceholder.typicode.com/posts/1"
23:28:21.452 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
23:28:21.452 [main] DEBUG o.s.web.client.RestTemplate - Writing [parameters] using [org.springframework.http.converter.StringHttpMessageConverter@3234e239]
23:28:21.855 [main] WARN o.s.web.client.RestTemplate - GET request for "https://jsonplaceholder.typicode.com/posts/1" resulted in 403 (Forbidden); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 403 Forbidden
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:598)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:556)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:454)
at restFulWS.Application.main(Application.java:21)
Updated: Header snapshot from postman
更新:来自邮递员的标题快照
Access-Control-Allow-Credentials →true
CF-Cache-Status →HIT
CF-RAY →2cfd2d4919e72dcd-BOM
Cache-Control →public, max-age=14400
Connection →keep-alive
Content-Encoding →gzip
Content-Type →application/json; charset=utf-8
Date →Tue, 09 Aug 2016 18:12:32 GMT
Etag →W/"134-PYMqYXMMQ68yDudiuhsVPg"
Expires →Tue, 09 Aug 2016 22:12:32 GMT
Pragma →no-cache
Server →cloudflare-nginx
Transfer-Encoding →chunked
Vary →Accept-Encoding
Via →1.1 vegur
X-Content-Type-Options →nosniff
X-Powered-By →Express
If someone can suggest, what i need to add in my code
如果有人可以建议,我需要在我的代码中添加什么
回答by P.J.Meisch
Try to add a "User-Agent" header to your request. You can either try to set a custom user agent value or use some value that identifies a Browser like "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
尝试在您的请求中添加“User-Agent”标头。您可以尝试设置自定义用户代理值或使用一些标识浏览器的值,例如“Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 ”
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
RestTemplate rt = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
String url = "https://jsonplaceholder.typicode.com/posts/1";
ResponseEntity<String> res = rt.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println(res);
}
}