用HttpClient写一个application/json的post请求
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class PostXml {
public static String doPost() throws IOException {
String json = "";
HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://127.0.0.1:8966/ms/");
StringEntity reqEntity = new StringEntity(
json, ContentType.create("application/json", "utf-8"));
httpPost.setEntity(reqEntity);
httpPost.addHeader("Accept", "application/json");
CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(
httpPost);
JSONObject object = null;
try {
HttpEntity entity = response.getEntity();
if (null != entity) {
String result = EntityUtils.toString(entity,"UTF-8");
object = JSONObject.parseObject(result);
System.out.println(object);
}
EntityUtils.consume(entity);
} finally {
response.close();
}
return object.toJSONString();
}
}