在做一个项目的过程中,发现调用HttpClients请求时,传参五花八门,有的用map,有的用流。代码重复量巨大,虽然在spring对应的框架已经封装,但对于没有依赖类似的jar包时,我考虑调用的问题,将请求封装为两个方法,post方法 和 get方法,方法参数为地址,和map参数,返回类型 为map字符串。代码如下,需要的时候直接传参获取返回参数。
package com.hb.kfcenter.kfSSO.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
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 com.alibaba.fastjson.JSON;
public class HttpclientUtil {
/**
* post请求
* @param url 请求地址
* @param param 参数 以map形式
* @return
*/
public static String post(String url, Map<String, String> param) {
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000).setSocketTimeout(6000)
.setConnectTimeout(6000).build();
httpPost.setConfig(requestConfig);
List<NameValuePair> nvps = new ArrayList<>();
if(param!=null&¶m.size()>0){
for (String key : param.keySet()) {
nvps.add(new BasicNameValuePair(key, String.valueOf(param.get(key))));
}
}
String data = "";
Map<String, String> map = new HashMap<String, String>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse resp = client.execute(httpPost);
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
data = EntityUtils.toString(he, "UTF-8");
map.put("code", "0");
map.put("data", data);
} else {
map.put("code", "1");
map.put("msg",
String.valueOf(resp.getStatusLine().getStatusCode()));
}
} catch (Exception e) {
map.put("code", "-1");
map.put("msg", "异常");
}
return JSON.toJSONString(map);
}
public static String get(String url, Map<String, String> param) {
HttpGet httpPost = new HttpGet(url);
CloseableHttpClient client = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000).setSocketTimeout(6000)
.setConnectTimeout(6000).build();
httpPost.setConfig(requestConfig);
StringBuffer params = new StringBuffer();
if(param!=null&¶m.size()>0){
for (String key : param.keySet()) {
if(StringUtils.isEmpty(params.toString())){
params.append("?"+key+"="+String.valueOf(param.get(key)));
}else{
params.append("&"+key+"="+String.valueOf(param.get(key)));
}
}
}
String data = "";
Map<String, String> map = new HashMap<String, String>();
try {
HttpResponse resp = client.execute(httpPost);
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
data = EntityUtils.toString(he, "UTF-8");
map.put("code", "0");
map.put("data", data);
} else {
map.put("code", "1");
map.put("msg",
String.valueOf(resp.getStatusLine().getStatusCode()));
}
} catch (Exception e) {
map.put("code", "-1");
map.put("msg", "异常");
}
return JSON.toJSONString(map);
}
}