依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
代码
package com.example.demo.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@Slf4j
public class HttpUtils {
public static String sendGet(String url){
log.info("sendGet url: {}", url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(60 * 1000)
.setConnectTimeout(60 * 1000)
.setSocketTimeout(60 * 1000).build();
httpGet.setConfig(requestConfig);
String resp = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
log.info("statusCode: {}",statusCode);
resp = EntityUtils.toString(response.getEntity());
log.info("httpClient response: {}", resp);
} catch (IOException e) {
log.error("Exception:", e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resp;
}
public static String sendPost(String url, String content){
log.info("sendPostForJson url: {}, Param: {}", url, content);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(60 * 1000)
.setConnectTimeout(60 * 1000)
.setSocketTimeout(60 * 1000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(content, ContentType.create("application/json", "utf-8")));
String resp = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
log.info("statusCode: {}",statusCode);
resp = EntityUtils.toString(response.getEntity());
log.info("httpClient response: {}", resp);
} catch (IOException e) {
log.error("Exception:", e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resp;
}
public static String sendPost(String url, String content, String contentType) {
log.info("sendPostForJson url: {}, Param: {}, contentType: {}", url, content, contentType);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(60 * 1000)
.setConnectTimeout(60 * 1000)
.setSocketTimeout(60 * 1000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", contentType);
httpPost.setEntity(new StringEntity(content, ContentType.create(contentType, "utf-8")));
String resp = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
log.info("statusCode: {}",statusCode);
resp = EntityUtils.toString(response.getEntity());
log.info("httpClient response: {}", resp);
} catch (IOException e) {
log.error("Exception:", e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resp;
}
}