首页 / JAVA / java – 获取403禁止错误
java – 获取403禁止错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 获取403禁止错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3303字,纯文字阅读大概需要5分钟。
内容图文
![java – 获取403禁止错误](/upload/InfoBanner/zyjiaocheng/765/cef5c1c1f77a4ec2b6b385cae03f30b6.jpg)
参见英文答案 > 403 Forbidden with Java but not web browser? 4个
获取403禁止使用以下代码,但“https://jsonplaceholder.typicode.com/posts/1”适用于邮递员
@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);
}
}
错误:
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)
更新:
来自邮递员的标题快照
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
如果有人可以建议,我需要在我的代码中添加什么
解决方法:
尝试在您的请求中添加“User-Agent”标头.您可以尝试设置自定义用户代理值,也可以使用一些标识浏览器的值,如“Mozilla / 5.0(Macintosh; Intel Mac OS X 10_11_6)AppleWebKit / 537.36(KHTML,如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);
}
}
内容总结
以上是互联网集市为您收集整理的java – 获取403禁止错误全部内容,希望文章能够帮你解决java – 获取403禁止错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。