Java 获取403禁止的错误

Java 获取403禁止的错误,java,spring,restful-url,resttemplate,Java,Spring,Restful Url,Resttemplate,下面的代码禁止使用403,尽管“”在postman中有效 @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { RestTemplate rt = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); heade

下面的代码禁止使用403,尽管“”在postman中有效

@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);
    }
}
更新: 来自邮递员的标题快照

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
如果有人可以建议,我需要在代码中添加什么

尝试向您的请求添加“用户代理”标题。您可以尝试设置自定义用户代理值,也可以使用一些标识浏览器的值,如“Mozilla/5.0(Macintosh;英特尔Mac OS X 10_11_6)AppleWebKit/537.36(KHTML,如Gecko)Chrome/51.0.2704.103 Safari/537.36”

@ComponentScan
@启用自动配置
公共类应用程序{
公共静态void main(字符串[]args){
RestTemplate rt=新的RestTemplate();
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
添加(“用户代理”、“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/54.0.2840.99 Safari/537.36”);
HttpEntity=新的HttpEntity(“参数”,标题);
字符串url=”https://jsonplaceholder.typicode.com/posts/1";
ResponseEntity res=rt.exchange(url,HttpMethod.GET,entity,String.class);
系统输出打印项次(res);
}
}

你的邮差标题值是多少?或者尝试ResponseEntity res=rt.exchange(url、HttpMethod.POST、entity、String.class);403禁止表示您的用户没有执行此操作的权限,或者没有用户有执行此操作的权限。如果这在
postman
上起作用,则在插入身份验证头时出现问题。GET/posts?id=1 HTTP/1.1主机:jsonplaceholder.typicode.com缓存控制:无缓存postman令牌:d96adb00-b143-8674-c32a-1ADE941D3EA来自postman@kuhajeyan
邮递员令牌
是邮递员使用的令牌不应影响
REST
功能。每次邮递员电话都会生成。使用Postman开发了一个HTTP java Rest调用后,我可以验证程序上不应该需要它。@sr7 Darn。我不确定那是什么。您可以使用postman生成代码,为您提供基础。单击
Save
下的
Generate code
链接,然后选择
Java OK HTTP
,它将大致显示请求在Java中的样子。这太棒了!!!但是,为什么???请(请!),解释一下。@AlmirCampos:没有关于这种行为的规范,至少我没有找到任何规范。我认为这是阻止爬虫程序和爬虫程序访问网站的一种尝试(实际上不起作用),因为这些程序(至少在早期)没有发送用户代理头。但正如你所看到的,这是很容易克服的,今天只有一个pita.Awesome@P.J.Meisch。你救了我的命。我被困在这上面超过三天了。这对我帮助很大!!!
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
@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);
    }
}