81,094
社区成员
发帖
与我相关
我的任务
分享
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("some url");
post.addHeader("Content-Type", "application/json; charset=utf-8");
String requestContent = produceRequestContent();//这个方法返回的是上述json字符串。
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(requestContent);
stringEntity.setContentEncoding("utf-8");
post.setEntity(stringEntity);
InputStream input = stringEntity.getContent();
byte[] buffer = new byte[input.available()];
input.read(buffer);
String testStr = new String(buffer);
System.out.println(requestContent);
System.out.println(testStr);
System.out.println(EntityUtils.toString(stringEntity,"iso-8859-1"));
System.out.println(EntityUtils.toString(stringEntity,"gb2312"));
System.out.println(EntityUtils.toString(stringEntity,"gbk"));
System.out.println(EntityUtils.toString(stringEntity,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
// 后面的execute省略了...