java Post请求总是报415
//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();
}
}
}