Java Jersey客户端REST API生成http 403错误

Java Jersey客户端REST API生成http 403错误,java,jersey-client,Java,Jersey Client,mine java项目上的Jersey客户端REST-API生成HTTP 403错误。尽管此项目在调用其他Restful API时运行良好,但除了伪的基于在线的REST APIJSONPlaceholder。请在以下代码中查找我的: public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Client client = Client.create(

mine java项目上的Jersey客户端REST-API生成HTTP 403错误。尽管此项目在调用其他Restful API时运行良好,但除了伪的基于在线的REST API
JSONPlaceholder
。请在以下代码中查找我的:

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Client client = Client.create();
    WebResource webResource =   client.resource("http://jsonplaceholder.typicode.com/posts");
    ClientResponse response = webResource.accept("application/json")
            .get(ClientResponse.class);
    if(response.getStatus() != 200) {
        throw new RuntimeException("Failed http error code :" + response.getStatus());
    }
    String output = response.getEntity(String.class);
    System.out.println(output);
错误:

当我昨天读到这个问题时,我本能地想到了用户代理标题。你的评论证明了这一点。为了给出一个更具可读性的答案,我将提供以下与您的示例相关的工作代码(尽管如此,我打赌不提供UA不是最好的方法;)


正如我在评论中提到的,在webResource实例的头中设置键
(“用户代理”)
和值
(“”)
,将引导解决方案。希望下面的代码片段能给你更好的想法

Client client = Client.create();
WebResource webResource = client.resource("http://jsonplaceholder.typicode.com/posts");
ClientResponse response = webResource.accept("application/json")
            .header("user-agent", "")
            .get(ClientResponse.class);
if(response.getStatus() != 200) {
    throw new RuntimeException("Failed http error code :" + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println(output);

谢谢大家的宝贵反馈。

API需要任何凭据吗?@peeskillet no man,它对所有浏览器都很好,但当我从java代码片段.Ureka调用它时,它无法工作!!它起作用了!我刚刚用“user-agent”参数{WebResource.accept(“application/json”).header(“user-agent”,“useragent”)}在WebResource实例上设置了header。就这样。
Client client = Client.create();
WebResource webResource = client.resource("http://jsonplaceholder.typicode.com/posts");
ClientResponse response = webResource.accept("application/json")
            .header("user-agent", "")
            .get(ClientResponse.class);
if(response.getStatus() != 200) {
    throw new RuntimeException("Failed http error code :" + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println(output);