jersey java 客户端返回 HTTP 错误代码:406

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11903613/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:51:51  来源:igfitidea点击:

jersey java client returning HTTP error code : 406

javarestjersey

提问by kamal

I am doing a REST Call to a Teamcity URI, to gte the lastSuccessful build Number but getting 406. If i use the same URI in Chrome's REST Console, i get the correct String ( Which is the latest Build Number

我正在对 Teamcity URI 进行 REST 调用,以获取 lastSuccessful 内部版本号但得到 406。如果我在 Chrome 的 REST 控制台中使用相同的 URI,我会得到正确的字符串(这是最新的内部版本号

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class LastSuccessBuildNum {

    public static void main(String[] args) {

        try {

            Client client = Client.create();
            // client basic auth demonstration 
            client.addFilter(new HTTPBasicAuthFilter("username", "password"));

            WebResource webResource = client
                    .resource("http://localteamcity.com/teamcity/app/rest/buildTypes/id:bt26/builds/status:SUCCESS/number");
            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 from Server .... \n");
            System.out.println(output);
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

stdout:

标准输出:

java.lang.RuntimeException: Failed : HTTP error code : 406
    at LastSuccessBuildNum.main(LastSuccessBuildNum.java:22)

回答by Elchin

Check the MIME type of the transfer in Chrome REST Client, maybe it is not json. 406 means that the server does not have a MIME type that the client accepts: http://www.checkupdown.com/status/E406.html

在 Chrome REST Client 中检查传输的 MIME 类型,可能不是 json。406表示服务器没有客户端接受的MIME类型:http: //www.checkupdown.com/status/E406.html

Is there a specific reason that you use jersey client instead of Apache Http Components?

您使用 jersey 客户端而不是 Apache Http Components 是否有特定原因?