将使用IDEA和maven完成本次接口请求实现,以下内容,将默认读者已完成环境准备,包括但不限于JDK、maven、IDEA,并熟练掌握他们的使用方法,如若有不清楚的请自行学习。
在IDEA中新建一个maven工程,pom.xml文件中引入httpclient、httpmime、httpcore
public static final Charset CHARSET_DEFAULT = StandardCharsets.UTF_8;
/**
* 无请求头信息的get()请求
*
* @param url API请求地址
* @return 请求响应完整信息
*/
public static CloseableHttpResponse get(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} catch (IOException e) {
LOGGER.error("get请求执行异常:\n");
e.printStackTrace();
}
return response;
}
/**
* 带请求头的get()请求
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @return 请求响应完整信息
*/
public static CloseableHttpResponse get(String url, HashMap headers) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
for (HashMap.Entry entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} catch (IOException e) {
LOGGER.error("get请求执行异常:\n");
e.printStackTrace();
}
return response;
}
/**
* 带请求头和额外参数的get()请求
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* 此方法需要所有的参数都在此传入不能一半直接写在url,一半在这里传参
* @return 请求响应完整信息
*/
public static CloseableHttpResponse get(String url, HashMap headers, List parameters) {
CloseableHttpClient httpClient = HttpClients.createDefault();
URLEncodedUtils.format(parameters, CHARSET_DEFAULT);
url = String.format("%s%s", url, new URIBuilder().addParameters(parameters));
HttpGet httpGet = new HttpGet(url);
for (HashMap.Entry entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} catch (IOException e) {
LOGGER.error("get请求执行异常:\n");
e.printStackTrace();
}
return response;
}
/**
* 无请求头,有额外参数的get()请求
*
* @param url API请求地址
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* 此方法需要所有的参数都在此传入不能一半直接写在url,一半在这里传参
* @return 请求响应完整信息
*/
public static CloseableHttpResponse get(String url, List parameters) {
CloseableHttpClient httpClient = HttpClients.createDefault();
URLEncodedUtils.format(parameters, CHARSET_DEFAULT);
url = String.format("%s%s", url, new URIBuilder().addParameters(parameters));
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} catch (IOException e) {
LOGGER.error("get请求执行异常:\n");
e.printStackTrace();
}
return response;
}
/**
* 无请求头,无参数信息的post()请求
*
* @param url API请求地址
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 带请求头、无参数的post()请求
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url, HashMap headers) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
for (HashMap.Entry entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 带请求头和参数的post()请求,通过表单(键值对)传参
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url, HashMap headers, List parameters) {
CloseableHttpClient httpClient = HttpClients.createDefault();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, CHARSET_DEFAULT);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
for (HashMap.Entry entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 无请求头,有额外参数的post()请求,通过表单(键值对)传参
*
* @param url API请求地址
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url, List parameters) {
CloseableHttpClient httpClient = HttpClients.createDefault();
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, CHARSET_DEFAULT);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 有请求头,有额外参数的post()请求,通过body传参
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @param bodyEntity body请求实体
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url, HashMap headers, String bodyEntity) {
CloseableHttpClient httpClient = HttpClients.createDefault();
StringEntity entity = new StringEntity(bodyEntity, CHARSET_DEFAULT);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
for (HashMap.Entry entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 无请求头,有额外参数的post()请求,通过body传参
*
* @param url API请求地址
* @param bodyEntity body请求实体
* @return 请求响应完整信息
*/
public static CloseableHttpResponse post(String url, String bodyEntity) {
CloseableHttpClient httpClient = HttpClients.createDefault();
StringEntity entity = new StringEntity(bodyEntity, CHARSET_DEFAULT);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("post请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 带有文件上传的post请求,只有请求头与文件信息,没有额外参数
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @param file 要上传的文件
* @param pairName 上传文件对应的参数名称
* @param fileType 上传文件对应的MIME类型,如 MIMEType.MIME_DOC
* @return 请求响应完整信息
*/
public static CloseableHttpResponse postUpload(String url, HashMap headers, File file, String pairName, String fileType) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求实体为浏览器兼容模式,防止上传中文文件名乱码
HttpMultipartMode mode = HttpMultipartMode.BROWSER_COMPATIBLE;
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(mode);
//实体中插入文件信息,设置编码方式
ContentType fileContentType = ContentType.create(fileType, CHARSET_DEFAULT);
multipartEntityBuilder.addBinaryBody(pairName, file, fileContentType, file.getName());
multipartEntityBuilder.setCharset(CHARSET_DEFAULT);
HttpEntity entity = multipartEntityBuilder.build();
httpPost.setEntity(entity);
for (HashMap.Entry entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("postUpload请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 带有文件上传的post请求,除请求头与文件信息外,还包含有额外参数
*
* @param url API请求地址
* @param headers 请求头,使用Map键值对方式传入
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* @param file 要上传的文件
* @param pairName 上传文件对应的参数名称
* @param fileType 上传文件对应的MIME类型,如 MIMEType.MIME_DOC
* @return 请求响应完整信息
*/
public static CloseableHttpResponse postUpload(String url, HashMap headers, List parameters, File file, String pairName, String fileType) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求实体为浏览器兼容模式,防止上传中文文件名乱码
HttpMultipartMode mode = HttpMultipartMode.BROWSER_COMPATIBLE;
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(mode);
//实体中插入文件信息,设置编码方式
ContentType fileContentType = ContentType.create(fileType, CHARSET_DEFAULT);
multipartEntityBuilder.addBinaryBody(pairName, file, fileContentType, file.getName());
multipartEntityBuilder.setCharset(CHARSET_DEFAULT);
//实体中插入其他表单参数信息
for (NameValuePair pair : parameters) {
ContentType contentType = ContentType.create("text/plain", CHARSET_DEFAULT);
StringBody value = new StringBody(pair.getValue(), contentType);
multipartEntityBuilder.addPart(pair.getName(), value);
// multipartEntityBuilder.addTextBody(pair.getName(),pair.getValue()) ; //使用这个会导致参数中文乱码
}
HttpEntity entity = multipartEntityBuilder.build();
httpPost.setEntity(entity);
//插入请求头信息
for (HashMap.Entry entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
CloseableHttpResponse response = null;
try {
//执行请求
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("postUpload请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
/**
* 带有文件上传的post请求,只文件信息和额外参数,没有请求头
*
* @param url API请求地址
* @param parameters 请求参数使用NameValuePair键值对方式传入,使用方式如下
* List parameters=new ArrayList<>();
* parameters.add(new BasicNameValuePair("page","2")) ;
* parameters.add(new BasicNameValuePair("token","tokenvalue")) ;
* @param file 要上传的文件
* @param pairName 上传文件对应的参数名称
* @param fileType 上传文件对应的MIME类型,如 MIMEType.MIME_DOC
* @return 请求响应完整信息
*/
public static CloseableHttpResponse postUpload(String url, List parameters, File file, String pairName, String fileType) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求实体为浏览器兼容模式,防止上传中文文件名乱码
HttpMultipartMode mode = HttpMultipartMode.BROWSER_COMPATIBLE;
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(mode);
//实体中插入文件信息,设置编码方式
ContentType fileContentType = ContentType.create(fileType, CHARSET_DEFAULT);
multipartEntityBuilder.addBinaryBody(pairName, file, fileContentType, file.getName());
multipartEntityBuilder.setCharset(CHARSET_DEFAULT);
//实体中插入其他表单参数信息
for (NameValuePair pair : parameters) {
ContentType contentType = ContentType.create("text/plain", CHARSET_DEFAULT);
StringBody value = new StringBody(pair.getValue(), contentType);
multipartEntityBuilder.addPart(pair.getName(), value);
// multipartEntityBuilder.addTextBody(pair.getName(),pair.getValue()) ; //使用这个会导致参数中文乱码
}
HttpEntity entity = multipartEntityBuilder.build();
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
//执行请求
response = httpClient.execute(httpPost);
} catch (IOException e) {
LOGGER.error("postUpload请求执行异常:" + url+ "\n");
e.printStackTrace();
}
return response;
}
put请求与post请求实现基本一致,把对应请求方式由post更换成put即可
delete请求与get请求实现基本一致,把对应的请求方式由get更换为delete即可