import net.sf.json.JSONArray;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 模拟post请求 json参数
* @param params json字符串
* @return
*/
public JSONArray getResponse(String params) {
String response = null;
String url = "";
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type","application/json");
StringEntity stringentity = new StringEntity(params,
ContentType.create("text/json", "UTF-8"));
httppost.setEntity(stringentity);
httppost.getAllHeaders();
httpresponse = httpclient.execute(httppost);
response = EntityUtils
.toString(httpresponse.getEntity());
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return JSONArray.fromObject(response);
}
模拟post请求 json格式传参
最新推荐文章于 2023-11-29 15:33:32 发布