先创建一个工具类,方便我们调用然后
package com.ninestars.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
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.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* CREATE BY Chaiyz ON 2022.04.12 14:23 星期二
* DESC:
*/
public class HttpClientUtil {
private int connectionRequestTimeout = 10000;
private int connectTimeout = 10000;
private int socketTimeout = 10000;
private String urlHead="";//接口头部
//post
public String sendPostMap(String postUrl, Map<String, Object> params,String type) throws Exception {
//type
// 1接收用@responsebody
// 2接收用@requestparam或者httpservletrequest.getParameter
//CloseableHttpClient httpClient = createSSLClientDefault();//https
CloseableHttpClient httpClient = HttpClients.createDefault();//http
try {
//实例化post方法
HttpPost httpost = new HttpPost(urlHead+postUrl);
config(httpost);
//提交参数
if("1".equals(type)){
String ms = String.valueOf(params);
StringEntity stringEntity = new StringEntity(String.valueOf(params),"UTF-8");
stringEntity.setContentType("application/json");
//将参数给post方法
httpost.setEntity(stringEntity);
}else{
List<NameValuePair> nvps = new ArrayList<>();
if(params != null){
Set<String> keySet = params.keySet();
for(String key:keySet){
nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
}
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps,"UTF-8");
uefEntity.setContentType("application/x-www-form-urlencoded");
//将参数给post方法
httpost.setEntity(uefEntity);
}
//执行post方法
CloseableHttpResponse response = httpClient.execute(httpost, HttpClientContext.create());
//获取返回值
HttpEntity entity = response.getEntity();
String reponseStr = EntityUtils.toString(entity, "utf-8");
return reponseStr;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//get
public String sendGetMap(String postUrl, Map<String, Object> params) throws Exception
{
//CloseableHttpClient httpClient = createSSLClientDefault();//https
CloseableHttpClient httpClient = HttpClients.createDefault();//http
try {
//提交参数
List<NameValuePair> nvps = new ArrayList<>();
if(params != null){
Set<String> keySet = params.keySet();
for(String key:keySet){
nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
}
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps,"UTF-8");
uefEntity.setContentType("application/x-www-form-urlencoded");
//将参数给get方法
String sparam = EntityUtils.toString(uefEntity);
//实例化get方法
HttpGet httget = new HttpGet(urlHead+postUrl+"?"+sparam);
config(httget);
//执行get方法
CloseableHttpResponse response = httpClient.execute(httget);
//获取返回值
HttpEntity entity = response.getEntity();
String reponseStr = EntityUtils.toString(entity, "utf-8");
return reponseStr;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* @param httpRequestBase
*/
private void config(HttpRequestBase httpRequestBase) {
httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");
httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");//"en-US,en;q=0.5");
httpRequestBase.setHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
httpRequestBase.setHeader("connection", "Keep-Alive");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build();
httpRequestBase.setConfig(requestConfig);
}
public JSONObject sendPostForm(String url, Map<String, Object> reqObj, String type) {
//type
// 1接收用@requestbody
// 2接收用@requestparam或者httpservletrequest.getParameter
try {
String str = sendPostMap(url, reqObj, type);
return JSONObject.parseObject(str);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public JSONObject sendGetForm(String url, Map<String, Object> reqObj) {
try {
String str = sendGetMap(url, reqObj);
return JSONObject.parseObject(str);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
然后我们就可以调用第三方接口了,没错。就是这么简单。看我演示
String url = "http://**/*/*"; //这里是你要调用的接口地址
Map<String, Object> reqObj = new HashMap<>();// 这里是你要传的参数
reqObj.put("terminalCode","CCBBAA0123456789");
reqObj.put("magazineUpdateTime","2010-01-01 00:00:00");
reqObj.put("cpage",1);
reqObj.put("pageSize",10);
reqObj.put("details",1);
JSONObject jsonObject = httpClientUtil.sendPostForm(url, reqObj,"2");
return jsonObject;// 现在就拿到接口的返回值了。
创作不易,如果有用记得点赞哦!