This commit is contained in:
lu7213 committed 2026-08-13 02:26:24 -04:00
1 parent cf9c801b93
commit f0b438eb6c
8 files changed
+158 -89

No files matched your search

@@ -41,7 +41,9 @@ import java.util.zip.GZIPInputStream;
public class HttpHelper {
private String proxyHost;
private Integer proxyPort;
private CloseableHttpClient httpsClient = null;
private CloseableHttpClient httpClient = null;
public String getProxyHost() {
return proxyHost;
}
@@ -57,7 +59,20 @@ public class HttpHelper {
public void setProxyPort(Integer proxyPort) {
this.proxyPort = proxyPort;
}
public void closeClient(){
try {
if (httpsClient != null) {
httpsClient.close();
httpsClient = null;
}
if (httpClient != null) {
httpClient.close();
httpClient = null;
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
public static void main(String[] args) throws Exception {
BigDecimal decimal = new BigDecimal("1").divide(new BigDecimal(100));
@@ -444,13 +459,19 @@ public class HttpHelper {
* @return
*/
public String getlHttpResult(String url, String method, Map<String, Object> headers, Map<String, Object> params) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpClient tempClient = null;
if (url.startsWith("https")) {
httpClient = getHttpsClient();
if(httpsClient == null){
httpsClient = getHttpsClient();
}
tempClient = httpsClient;
} else {
httpClient = getHttpClient();
if(httpClient == null) {
httpClient = getHttpClient();
}
tempClient = httpClient;
}
CloseableHttpResponse response = this.send(httpClient, url, method, headers, params);
CloseableHttpResponse response = this.send(tempClient, url, method, headers, params);
if (method.toLowerCase().equals("options")) {
return response.getStatusLine().getStatusCode() + "";
} else {
@@ -466,24 +487,36 @@ public class HttpHelper {
}
public String getlHttpResultCode(String url, String method, Map<String, Object> headers, Map<String, Object> params) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpClient tempClient = null;
if (url.startsWith("https")) {
httpClient = getHttpsClient();
if(httpsClient == null){
httpsClient = getHttpsClient();
}
tempClient = httpsClient;
} else {
httpClient = getHttpClient();
if(httpClient == null) {
httpClient = getHttpClient();
}
tempClient = httpClient;
}
CloseableHttpResponse response = this.send(httpClient, url, method, headers, params);
CloseableHttpResponse response = this.send(tempClient, url, method, headers, params);
return response.getStatusLine().getStatusCode() + "";
}
public Boolean testHttp(String url, String method, Map<String, Object> headers, Map<String, Object> params) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpClient tempClient = null;
if (url.startsWith("https")) {
httpClient = getHttpsClient();
if(httpsClient == null){
httpsClient = getHttpsClient();
}
tempClient = httpsClient;
} else {
httpClient = getHttpClient();
if(httpClient == null) {
httpClient = getHttpClient();
}
tempClient = httpClient;
}
CloseableHttpResponse response = this.send(httpClient, url, method, headers, params);
CloseableHttpResponse response = this.send(tempClient, url, method, headers, params);
boolean result = response.getStatusLine().getStatusCode() == 200;
response.close();
return result;
@@ -494,11 +527,17 @@ public class HttpHelper {
}
public String playLoad(String method, String url, Map<String, Object> headersMap, Object data, String contentType, String charset) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpClient tempClient = null;
if (url.startsWith("https")) {
httpClient = getHttpsClient();
if(httpsClient == null){
httpsClient = getHttpsClient();
}
tempClient = httpsClient;
} else {
httpClient = getHttpClient();
if(httpClient == null) {
httpClient = getHttpClient();
}
tempClient = httpClient;
}
String result = null;
HttpEntityEnclosingRequestBase httpEntityEnclosingRequestBase = null;
@@ -532,7 +571,7 @@ public class HttpHelper {
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = (CloseableHttpResponse) httpClient.execute(httpEntityEnclosingRequestBase);
response = (CloseableHttpResponse) tempClient.execute(httpEntityEnclosingRequestBase);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());