java http请求 httpClient
第三方jar包导出(maven)
jar包下载链接(可导入)
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
HttpUtils工具类
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
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.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
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.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author zhong
* http 请求utils
*/
public class HttpUtils {
private static HttpEntity getHttpEntity(HttpRequestBase request, Map<String, String> header) {
CloseableHttpClient httpclient = HttpClients.createDefault();
// 设置头部信息
if (header != null) {
for (Map.Entry<String, String> vo : header.entrySet()) {
request.setHeader(vo.getKey(), vo.getValue());
}
}
try {
CloseableHttpResponse response = httpclient.execute(request);
return response.getEntity();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String get(String url, Map<String, String> params, Map<String, String> header) throws IOException, CloneNotSupportedException {
// 判断是否带参数
if (params != null) {
List<BasicNameValuePair> paramsList = new ArrayList<>();
//将参数进行循环
for (Map.Entry<String, String> vo : params.entrySet()) {
paramsList.add(new BasicNameValuePair(vo.getKey(), vo.getValue()));
}
// 进行URL拼接
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(paramsList, Consts.UTF_8));
}
HttpGet httpGet = new HttpGet(url);
HttpEntity entity = getHttpEntity(httpGet, header);
if(entity!=null){
return EntityUtils.toString(entity);
}
return null;
}
public static String post(String url, Map<String, Object> data, Map<String, String> header) throws IOException {
HttpPost httpPost = new HttpPost(url);
// 判断是否带参数
if (data!= null) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(StandardCharsets.UTF_8);
//将参数进行循环
for (Map.Entry<String, Object> vo : data.entrySet()) {
if (vo.getValue() instanceof File) {
builder.addBinaryBody(vo.getKey(), (File) vo.getValue());
} else if (vo.getValue() instanceof String) {
builder.addTextBody(vo.getKey(), (String) vo.getValue());
}
}
httpPost.setEntity(builder.build());
}
HttpGet httpGet = new HttpGet(url);
HttpEntity entity = getHttpEntity(httpGet, header);
if(entity!=null){
return EntityUtils.toString(entity);
}
return null;
}
}