Http状态码406(Not Acceptable)

HTTP状态码分类

分类分类描述
1**信息,服务器收到请求,需要请求者继续执行操作
2**成功,操作被成功接收并处理
3**重定向,需要进一步的操作以完成请求
4**客户端错误,请求包含语法错误或无法完成请求
5**服务器错误,服务器在处理请求的过程中发生了错误

HTTP 406 错误指无法接受 (Not acceptable)错误。

如果Web服务器检测到其想要反馈的数据不能被客户端接受,其将反馈带有406错误代码的标头。 客户端可以向Web服务器指示其将接受服务器反馈数据的特征,这是通过使用以下类型的“接受标头”完成的:

接受:客户端接受的MIME(多功能Internet邮件扩展服务)类型。 例如,浏览器可能只接受知道如何处理的反馈数据类型(HTML文件,GIF文件等)。

1、原始的代码:

public static String requestPost(String url, JSONObject params) {
    //CloseableHttpClient httpClient = HttpClients.createDefault();
    String responseResult = "";
    try {
        HttpClient httpClient = HttpsClient.getInstance();
        // 1 创建一个post对象
        HttpPost post = new HttpPost(url);
        post.addHeader("Content-type", "application/json; charset=utf-8");
        post.setHeader("Accept", "application/json");
        // 2 创建一个Entity。模拟一个xml
        log.info("HttpsClient.requestPost.params-->>" + params);
        StringEntity stringEntity = new StringEntity(params.toJSONString(), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        post.setEntity(stringEntity);
        // 3 执行post请求
        HttpResponse response = httpClient.execute(post);
        //CloseableHttpResponse response = httpClient.execute(post);
        HttpEntity he = response.getEntity();
        responseResult = EntityUtils.toString(he, "UTF-8");
        log.info("HttpsClient.requestPost.responseResult-->>" + responseResult);
        //response.close();
        //httpClient.close();
    } catch (Exception e) {
        e.printStackTrace();
        log.info("HttpsClient.requestPost.Exception-->>" + e.getMessage());
    }
    return responseResult;
}

2、注解掉报错的代码

public static String requestPost(String url, JSONObject params) {
    //CloseableHttpClient httpClient = HttpClients.createDefault();
    String responseResult = "";
    try {
        HttpClient httpClient = HttpsClient.getInstance();
        // 1 创建一个post对象
        HttpPost post = new HttpPost(url);
        post.addHeader("Content-type", "application/json; charset=utf-8");
        //post.setHeader("Accept", "application/json");
        // 2 创建一个Entity。模拟一个xml
        log.info("HttpsClient.requestPost.params-->>" + params);
        StringEntity stringEntity = new StringEntity(params.toJSONString(), "UTF-8");
        stringEntity.setContentEncoding("UTF-8");
        post.setEntity(stringEntity);
        // 3 执行post请求
        HttpResponse response = httpClient.execute(post);
        //CloseableHttpResponse response = httpClient.execute(post);
        HttpEntity he = response.getEntity();
        responseResult = EntityUtils.toString(he, "UTF-8");
        log.info("HttpsClient.requestPost.responseResult-->>" + responseResult);
        //response.close();
        //httpClient.close();
    } catch (Exception e) {
        e.printStackTrace();
        log.info("HttpsClient.requestPost.Exception-->>" + e.getMessage());
    }
    return responseResult;
}

3、解决方案

注解掉://post.setHeader("Accept", "application/json")

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值