1
獲得403禁止下面的代碼,雖然 「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);
}
}
錯誤:
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 [[email protected]34e239]
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
如果有人可以建議,我需要在我的代碼中添加
什麼是您的發帖人頭值? 或嘗試ResponseEntity res = rt.exchange(url,HttpMethod.POST,entity,String.class); –
kuhajeyan
403禁止表示您的用戶無權執行此操作,也沒有用戶有權執行此操作。如果這是對'郵遞員'的工作,那麼當你插入你的驗證頭時會出現問題。 –
GET /帖ID = 1 HTTP/1.1 主機:?jsonplaceholder.typicode.com 緩存控制:無緩存 郵差令牌:d96adb00-b143-8674-C32A-1ade941d33ea 是從郵遞員 – sr7