package cn.stylefeng.guns.core.util; import java.io.IOException; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient;; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; public class HttpClientUtils { public static String doPost(String url, Map<String, String> headers, String body) throws IOException{ HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); for (Map.Entry<String, String> e : headers.entrySet()) { httpPost.addHeader(e.getKey(), e.getValue()); } httpPost.setEntity(new StringEntity(body, "UTF-8")); HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity,"UTF-8"); return result; } }
HttpClient(HttpPost)调取第三方接口
最新推荐文章于 2023-02-17 15:18:06 发布