package com.nq.utils.http;
|
|
|
import com.google.gson.Gson;
|
import com.nq.utils.PropertiesUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.ClientProtocolException;
|
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.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.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
import org.junit.platform.commons.logging.Logger;
|
import org.junit.platform.commons.logging.LoggerFactory;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.*;
|
|
@Slf4j
|
public class HttpClientRequest {
|
|
// 可配置的超时参数(单位:毫秒)
|
private static final int CONNECT_TIMEOUT = 35000;
|
private static final int CONNECTION_REQUEST_TIMEOUT = 35000;
|
private static final int SOCKET_TIMEOUT = 60000;
|
|
public static String doGet(String url) {
|
// 1. 参数校验
|
if (url == null || url.trim().isEmpty()) {
|
throw new IllegalArgumentException("URL cannot be null or empty");
|
}
|
|
// 2. 创建HTTP客户端和请求对象
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
HttpGet httpGet = new HttpGet(url);
|
|
// 3. 配置请求参数
|
RequestConfig requestConfig = RequestConfig.custom()
|
.setConnectTimeout(CONNECT_TIMEOUT)
|
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
|
.setSocketTimeout(SOCKET_TIMEOUT)
|
.build();
|
httpGet.setConfig(requestConfig);
|
|
// 4. 执行请求并处理响应
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
int statusCode = response.getStatusLine().getStatusCode();
|
if (statusCode != 200) {
|
throw new IOException("HTTP request failed with status code: " + statusCode);
|
}
|
|
HttpEntity entity = response.getEntity();
|
if (entity == null) {
|
throw new IOException("Response entity is null");
|
}
|
|
return EntityUtils.toString(entity);
|
}
|
} catch (IOException e) {
|
throw new RuntimeException("HTTP request failed", e);
|
}
|
}
|
|
public static String doPost(String url, Map<String, Object> paramMap) {
|
CloseableHttpClient httpClient = null;
|
CloseableHttpResponse httpResponse = null;
|
String result = "";
|
|
httpClient = HttpClients.createDefault();
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
|
|
httpPost.setConfig(requestConfig);
|
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
if (null != paramMap && paramMap.size() > 0) {
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
Set<Map.Entry<String, Object>> entrySet = paramMap.entrySet();
|
|
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
|
while (iterator.hasNext()) {
|
Map.Entry<String, Object> mapEntry = (Map.Entry) iterator.next();
|
nvps.add(new BasicNameValuePair((String) mapEntry.getKey(), mapEntry.getValue().toString()));
|
}
|
|
|
try {
|
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
|
try {
|
httpResponse = httpClient.execute(httpPost);
|
|
HttpEntity entity = httpResponse.getEntity();
|
result = EntityUtils.toString(entity);
|
} catch (ClientProtocolException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
|
if (null != httpResponse) {
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (null != httpClient) {
|
try {
|
httpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return result;
|
}
|
|
public static String doPostJson(String url, Map<String, String> paramMap) {
|
CloseableHttpClient httpClient = null;
|
CloseableHttpResponse httpResponse = null;
|
String result = "";
|
|
httpClient = HttpClients.createDefault();
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
|
httpPost.setConfig(requestConfig);
|
httpPost.addHeader("Content-Type", "application/json");
|
StringEntity reqEntity = new StringEntity(new Gson().toJson(paramMap),"utf-8");
|
httpPost.setEntity(reqEntity);
|
try {
|
httpResponse = httpClient.execute(httpPost);
|
|
HttpEntity entity = httpResponse.getEntity();
|
result = EntityUtils.toString(entity);
|
} catch (ClientProtocolException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
|
if (null != httpResponse) {
|
try {
|
httpResponse.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (null != httpClient) {
|
try {
|
httpClient.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return result;
|
}
|
|
|
}
|