java Post请求总是报415

z563394688 2018-07-13 03:56:27
//java后台发的请求为什么总是报415,数据格式没问题啊
public static void main(String[] arg) {
HttpPost post = null;
try {
HttpClient httpClient = new DefaultHttpClient();
post = new HttpPost(url);
post.setHeader("Content-type", "application/json;charset=utf-8");
post.setHeader("Connection", "Close");
String sessionId = getSessionId();
post.setHeader("SessionId", sessionId);

StringEntity entity = new StringEntity(jsonObj, java.nio.charset.Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/json");
post.setEntity(entity);
System.out.println("post===" + post);
HttpResponse response = httpClient.execute(post);

// 检验返回码
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
//LogUtil.info("请求出错: "+statusCode);
System.out.println("请求出错: " + statusCode);
} else {
int retCode = 0;
String sessendId = "";
// 返回码中包含retCode及会话Id
for (Header header : response.getAllHeaders()) {
if (header.getName().equals("retcode")) {
retCode = Integer.parseInt(header.getValue());
}
if (header.getName().equals("SessionId")) {
sessendId = header.getValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (post != null) {
try {
post.releaseConnection();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
...全文
1619 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
i9577 2020-08-06
  • 打赏
  • 举报
回复 1
我今天也遇到了,是这个的问题,去掉就解决了
entity.setContentEncoding("UTF-8");
哈希塞特 2018-08-11
  • 打赏
  • 举报
回复
http 4打头的状态码都是代表客户端错误,5打头的状态码都是服务端错误
qq_35007940 2018-07-17
  • 打赏
  • 举报
回复
跨域 搜一下吧
stacksoverflow 2018-07-13
  • 打赏
  • 举报
回复
把这句话删掉试一下
post.setHeader("Content-type", "application/json;charset=utf-8");

------------------------
新开知识星球(ID:7660766),欢迎大家捧场。
Java学习不走弯路-让初学者不走弯路简单快捷的完成Java的学习。
------------------------

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧