最近在和第三方对接,经常使用PostUtil,现在分享一下工具类
主要提供
1.请求头参数允许JsonObject,String,Map<String,Object>,Map<String, String>
2.contentType的类型区别
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.*;
public class PostUtil {
private static final String CHARSET = "UTF-8";
public static String post(String url, String params) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity sEntity = new StringEntity(params, CHARSET);
httpPost.setEntity(sEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, CHARSET);
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static String postObject(String url, Map<String,Object> parammap) {
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : parammap.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
// 调用接口返回的字符串
return EntityUtils.toString(entity, "UTF-8");
}
} finally {
response.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static String postJson(String url, String json){
return postJSONWithHeaders(url, json, null);
}
public static String postJSONWithHeaders(String url, String json, Map<String, String> headers) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity sEntity = new StringEntity(json, CHARSET);
httpPost.setEntity(sEntity);
httpPost.setHeader("Content-Type","application/json;charset=utf-8");
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, CHARSET);
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static String post(String url, Map<String,String> params) {
return postForm(url, params, null);
}
public static String postForm(String url, Map<String, String> params, String contentType) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
if (contentType != null) {
httpPost.setHeader("Content-Type",contentType);
}
List<NameValuePair> nameValuePairs = new LinkedList<>();
for (String key : params.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs,CHARSET);
httpPost.setEntity(urlEncodedFormEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, CHARSET);
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public static String postFormData(String url, Map<String, String> params) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type","application/form-data");
List<NameValuePair> nameValuePairs = new LinkedList<>();
for (String key : params.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs,CHARSET);
httpPost.setEntity(urlEncodedFormEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, CHARSET);
}
} finally {
response.close();
httpClient.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
}
应该挺全的,
日拱一卒,得寸进寸!!!