如何解决java中的响应代码406错误?

如何解决java中的响应代码406错误?,java,json,https,outlook,Java,Json,Https,Outlook,我可以使用microsoft outlook api进行rest调用。这是我写的代码 public static void sendGet() { String url = "https://outlook.office365.com/api/v1.0/me/folders/Inbox/messages"; final String CONTENT_TYPE = "application/json"; final String ACCEPT_LANGUAGE = "en

我可以使用microsoft outlook api进行rest调用。这是我写的代码

public static void sendGet() {

    String url = "https://outlook.office365.com/api/v1.0/me/folders/Inbox/messages";
    final String CONTENT_TYPE = "application/json";
    final String ACCEPT_LANGUAGE = "en-US,en;q=0.8";

    try {

        URL obj = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();

        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-Type", CONTENT_TYPE);
        connection.setRequestProperty("Accept-Language", ACCEPT_LANGUAGE);
        connection.setRequestProperty("Authorization", "Basic c2h1YW5nQHZpdfdGVjaGluYydf5jb2fdXjhNCE="); //base64 encoding of auth username:password

        int responseCode = connection.getResponseCode();
        System.out.println("response code: " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String inputLine;
        StringBuffer response = new StringBuffer();

        while((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();

        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
我试图返回一个包含收件箱中所有消息的json响应,但它返回一个IO异常,错误代码为406

我非常确信内容类型“application/json”是受支持的,当我使用postman执行rest调用时,它将能够成功地返回json数据

从postman头中,支持application/json的内容类型。有人知道我做错了什么吗

根据请求中发送的Accept标头,请求的资源只能生成不可接受的内容

换句话说,将API返回的内容类型添加到accept头中

根据请求中发送的Accept标头,请求的资源只能生成不可接受的内容


换句话说,将API返回的内容类型添加到accept标头。

您是否尝试将
accept:application/json
添加到HTTP标头

Accept
头被HTTP客户端用来告诉服务器他们接受什么内容类型。然后,服务器发回一个响应,其中包括一个
Content-Type
头,告诉客户端返回内容的实际内容类型


tldr
Accept
是客户端能够消费的内容,而
内容类型
是数据的实际内容。

您是否尝试将
Accept:application/json
添加到HTTP头中

Accept
头被HTTP客户端用来告诉服务器他们接受什么内容类型。然后,服务器发回一个响应,其中包括一个
Content-Type
头,告诉客户端返回内容的实际内容类型


tldr
Accept
是客户端能够消费的内容,而
Content-Type
是数据的实际内容。

Accept和Content-Type有什么不同。我认为内容类型:application/json会起作用。@mrtofu内容类型标题描述您正在发送的内容,而accept标题描述您希望从服务器接收的内容。accept和content-type之间有什么不同。我认为content-type:application/json会起作用。@mrtofu content-type头描述您正在发送的内容,而accept头描述您希望从服务器接收的内容。