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")