HttpUtils工具类
- 常见的几种请求方式
- httpcomponent
- resttemplate
- okhhttp3
import com.ectrip.b2b.common.log.ExceptionLog;
import org.apache.http.HttpEntity;
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.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;
import java.util.Map;
public class HttpUtils {
private static final String DEFAULT_CHARSET = "UTF-8";
private static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
private static final int DEFAULT_SOCKET_TIMEOUT = 60000;
public static String doGet(String apiUrl) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpGet httpGet = new HttpGet(apiUrl);
CloseableHttpResponse response = null;
try {
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
return httpStr;
}
public static String doGet(String apiUrl, Map<String, String> headers) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpGet httpGet = new HttpGet(apiUrl);
CloseableHttpResponse response = null;
try {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
return httpStr;
}
public static String doPostJson(String apiUrl, String json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
try {
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.build();
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(json, "UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
return httpStr;
}
public static String doPostJson(String apiUrl, String json, Map<String, String> headers) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
try {
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.build();
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(json, "UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
ExceptionLog.print(e.getMessage(), e);
}
}
return httpStr;
}
}