ApiHttpUtils
url是请求的地址,params是请求的参数
然后拿到result再对数据进行处理
import com.alibaba.fastjson.JSON;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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;
import java.io.IOException;
import java.util.Map;
public class ApiHttpUtils {
public static String getHttpData(String url, Map params) {
String result="";
try{
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(JSON.toJSONString(params)));
// httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
if(response!=null){
result = EntityUtils.toString(response.getEntity(), "UTF-8");
}
client.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}