java.io.IOException Server returned HTTP response code 415

在做项目的时候,需要需要请求另一个项目的接口,需要用到http://去请求,于是就遇到了这个错误:java.io.IOException Server returned HTTP response code 415 ,当时自己感觉代码写的没有问题呀(好吧,是我太自恋了),于是上网查了好多资料,总结出以下三点会出现这个错误:

1.http://请求的路径是否一致

2.查看你的 http 请求方法,以及服务器端的设置比如:有一个 强制用 post 请求的接口,你是不是用了非 post 请

3.psot请求参数设置,是不是必须json格式

好了先看下我没有解决bug之前的代码,如下:

String url1=propertyResolver.getProperty("custom.cert.url");
String url=url1+"/sendCustomTextMessage";
ReviewResult reviewResult = new ReviewResult();
reviewResult.setOpenid(review.getOpenid());
reviewResult.setContent(content);
//将对像转成json格式
String json = StringUtil.object2Json(reviewResult);
//请求接口
ApiResult apiResult = ApiResult.create(HttpUtil.post(url,json));
if(apiResult.getInt("status") == 1){
    log.info("------------请求客服消息接口成功------------");
} else {
    log.info("------------请求客服消息接口失败------------");
}

这时测试的时候报了 java.io.IOException Server returned HTTP response code 415 这个错误

下面是我修改之后的代码:

// 发送客服消息
String url1=propertyResolver.getProperty("custom.cert.url");
String url=url1+"/sendCustomTextMessage";
ReviewResult reviewResult = new ReviewResult();
reviewResult.setOpenid(review.getOpenid());
reviewResult.setContent(content);
//将对像转成json格式
String json = StringUtil.object2Json(reviewResult);
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type","application/json");
//请求接口
ApiResult apiResult = ApiResult.create(HttpUtil.post(url,json, header));
if(apiResult.getInt("status") == 1){
    log.info("------------请求客服消息接口成功------------");
} else {
    log.info("------------请求客服消息接口失败------------");
}
可以看到我添加了"Content-Type","application/json" 这句话,原因就是应为我没有明确json格式,加了这么一段话,于是很顺利的就请求了接口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章