
为什么使用HttpClient发送json总是返回400
2017-02-21 · 做真实的自己 用良心做教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注

展开全部
正好我这两天工作涉及到 java后台 json的交互
此代码肯定可以用:
public static String httpCall(String url, String json) {
String content ="";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Content-type", "application/json;charset=utf-8");
StringEntity postingString = new StringEntity(json,"UTF-8");
postingString.setContentEncoding("utf-8");
// json传递
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
content = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
另外,建议用工具测试一下,确认接口能通。
此代码肯定可以用:
public static String httpCall(String url, String json) {
String content ="";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Content-type", "application/json;charset=utf-8");
StringEntity postingString = new StringEntity(json,"UTF-8");
postingString.setContentEncoding("utf-8");
// json传递
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
content = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
另外,建议用工具测试一下,确认接口能通。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询