纯API使用,直接贴 代码
pom依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
package com.lx.httpClientUtils;
import org.apache.http.Header;
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.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
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.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
private final static int connectionTimeOut = 60 * 1000;
private final static int socketTimeOut = 60 * 1000;
private static RequestConfig config = RequestConfig.custom()
.setSocketTimeout(socketTimeOut)
.setConnectTimeout(connectionTimeOut)
.setConnectionRequestTimeout(connectionTimeOut)
.build();
/**
* @param httpPost
* @return
*/
private static String sendPost(HttpPost httpPost) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
//执行请求
try {
//创建默认的httpClient实例
httpClient = HttpClients.createDefault();
httpPost.setConfig(config);
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "utf-8");
if (response.getStatusLine().getStatusCode() != 200) {
throw new Exception(responseContent);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* @param url
* @param params
* @return
*/
private static String sendPost(String url, String params) {
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(params, "UTF-8");
stringEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(stringEntity);
return sendPost(httpPost);
}
/**
* @param url
* @param maps
* @return
*/
private static String sendPost(String url, Map<String, String> maps) {
HttpPost httpPost = new HttpPost(url);
//创建参数队列
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return sendPost(httpPost);
}
/**
* @param url
* @param maps
* @param files
* @return
* @throws UnsupportedEncodingException
*/
private static String sendPost(String url, Map<String, String> maps, List<File> files) throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();
for (String key : maps.keySet()) {
multipartBuilder.addPart(key, new StringBody(URLEncoder.encode(maps.get(key), "UTF-8"), ContentType.TEXT_PLAIN));
}
if (files != null) {
for (File file : files) {
FileBody fileBody = new FileBody(file);
multipartBuilder.addPart("files", fileBody);
}
}
HttpEntity httpEntity = multipartBuilder.build();
httpPost.setEntity(httpEntity);
return sendPost(httpPost);
}
/**
* 返回值中有文件存在
* @param url
* @param maps
* @param localPathFile
*/
private static void sendPostFile(String url, Map<String, String> maps, String localPathFile) {
HttpPost httpPost = new HttpPost(url);
//创建参数队列
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
}
InputStream content = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
//创建服务端,打开连接
httpClient = HttpClients.createDefault();
httpPost.setConfig(config);
//发起请求
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != 200) {
throw new Exception(responseContent);
}
entity = response.getEntity();
content = entity.getContent();
bis = new BufferedInputStream(content);
Header header = entity.getContentType();
if ("multipart/form-data;charset=UTF-8".equals(header.getValue())) {//读取文件
System.out.println("开始获取文件");
bos = new BufferedOutputStream(new FileOutputStream(localPathFile));
int len;
byte[] buffer = new byte[1024];
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} else {
System.out.println("无文件");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//关闭流
if (bos != null) {
bos.close();
}
if (bis!=null){
bis.close();
}
//关闭连接
if (response!=null){
response.close();
}
if (httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}