httpclient版本:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
1.get请求
/**
* @Title: sendGet
* @Description: get请求
* @param: @param url 请求url
* @param: @param headers 请求头
*/
public static JSONObject sendGet(String url,Map headers) throws Exception
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
//请求头
if(headers!=null && headers.size()>0){
Set keySet = headers.keySet();
for(Object key : keySet){
httpGet.addHeader(new BasicHeader((String)key, (String)headers.get(key)));
}
}
HttpResponse response = httpClient.execute(httpGet);
return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
}
2. post请求
2.1 post请求(不带参数)
/**
* @Title: sendPostWithoutParamName
* @Description: 无参数名,直接传数据
* @param: url 请求的url
* @param: headers 请求头
* @param: paramValue 要传送的内容
*/
public static JSONObject sendPostWithoutParamName(String url,Map headers,String paramValue) throws Exception
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求头
if(headers!=null && headers.size()>0){
Set keySet = headers.keySet();
for(Object key : keySet){
httpPost.addHeader(new BasicHeader((String)key, (String)headers.get(key)));
}
}
StringEntity paramEntity = new StringEntity(paramValue,"utf-8"); //防止中文乱码
httpPost.setEntity(paramEntity);
HttpResponse response = httpClient.execute(httpPost);
return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
}
2.1 .post请求(带参数,参数中没有文件)
/**
* @Title: sendPostWithParamName
* @Description: 带参数名的多参数 post请求
* @param: url
* @param: headers 请求头
* @param: params map类型,key是参数名 value是参数值
*/
public static JSONObject sendPostWithParamName(String url,Map headers,Map params) throws Exception
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//请求头
if(headers!=null && headers.size()>0){
Set keySet = headers.keySet();
for(Object key : keySet){
httpPost.addHeader(new BasicHeader((String)key, (String)headers.get(key)));
}
}
//参数
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
if(params!=null && params.size()>0){
Set keySet = params.keySet();
for(Object key : keySet)
{
paramsList.add(new BasicNameValuePair((String)key, (String)params.get(key)));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, "utf-8"));
HttpResponse response = httpClient.execute(httpPost);
return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
}
2.2 post请求,带参数且参数中含有 文件
/**
* @Title: uploadFile
* @Description: 上传文件
* @param: url 接口
* @param: headers 请求头
* @param: params 必要的字符串参数
* @param: file 文件
*/
public static JSONObject uploadFile(String url,Map headers,Map params,File file) throws Exception, IOException
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//请求头
if(headers!=null && headers.size()>0){
Set keySet = headers.keySet();
for(Object key : keySet){
httpPost.addHeader(new BasicHeader((String)key, (String)headers.get(key)));
}
}
//常规参数
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if(params!=null && params.size()>0){
Set keySet = params.keySet();
for(Object key : keySet)
{
builder.addTextBody((String)key, (String)params.get(key));
}
}//文件
builder.addBinaryBody("media",file); //media是参数名,根据后端接口的规定自行更改
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
}
json串和java对象之间转换使用 的jar包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>