public static String getDataDemo(String url) throws ClientProtocolException,IOException,BusiException{
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse addResponse = client.execute(httpGet);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity, "utf-8");
client.close();
return result;
}
package com.pcitc.fms.dconf.util.remoteCall;
import java.io.IOException;
import java.net.SocketTimeoutException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pcitc.imp.common.exception.BusiException;
/**
* @Description: YLCloseableHttpClient
* @Date: 2023/5/31
* @return:
* @param:
**/
public class YLCloseableHttpClient {
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static final int DEF_REQUEST_TIMEOUT = 30000;
/**
* @Description: post
* @Date: 2023/5/31
* @return: java.lang.String
* @param: [url, pojo]
**/
public static String post(String url, String pojo) throws ClientProtocolException, IOException,
BusiException {
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(pojo, "utf-8");
httpPost.setEntity(entity);
CloseableHttpResponse addResponse = client.execute(httpPost);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity);
System.out.println(result);
client.close();
return result;
}
/**
* @Description: put
* @Date: 2023/5/31
* @return: java.lang.String
* @param: [url, pojo]
**/
public static String put(String url, String pojo) throws ClientProtocolException, IOException,
BusiException {
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
StringEntity entity = new StringEntity(pojo, "utf-8");
httpPut.setEntity(entity);
CloseableHttpResponse addResponse = client.execute(httpPut);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity);
System.out.println(result);
client.close();
return result;
}
/**
* @Description: delete
* @Date: 2023/5/31
* @return: java.lang.String
* @param: [url]
**/
public static String delete(String url) throws ClientProtocolException, IOException,
BusiException {
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpDelete httpDelet = new HttpDelete(url);
CloseableHttpResponse addResponse = client.execute(httpDelet);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity);
System.out.println(result);
client.close();
return result;
}
/**
* @Description: get
* @Date: 2023/5/31
* @return: boolean
* @param: [url]
**/
public static boolean get(String url) throws ClientProtocolException, IOException,
BusiException {
String result = "";
boolean hasData = false;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse addResponse = client.execute(httpGet);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity, "utf-8");
if (StringUtils.isNotEmpty(result)) {
JSONObject jSONObject = JSONObject.parseObject(result);
JSONObject object1 = jSONObject.getJSONObject("collection");
JSONArray jsonArray = object1.getJSONArray("items");
if (!jsonArray.isEmpty()) {
hasData = true;
}
System.out.println(result);
client.close();
}
return hasData;
}
/**
* @Description: getDate
* @Date: 2023/5/31
* @return: java.lang.String
* @param: [url]
**/
public static String getDate(String url) throws ClientProtocolException, IOException,
BusiException {
String result = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse addResponse = client.execute(httpGet);
HttpEntity responseEntity = addResponse.getEntity();
result = EntityUtils.toString(responseEntity, "utf-8");
client.close();
return result;
}
public static Object getObject(String url) throws ClientProtocolException, IOException,
BusiException {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
String result = EntityUtils.toString(responseEntity, "utf-8");
client.close();
return result;
}
@SuppressWarnings("resource")
public static String doHttpsGet(String url) throws Exception {
HttpClient httpClient = null;
HttpGet httpGet = null;
String result = null;
httpClient = new SSLClient();
httpGet = new HttpGet(url);
httpGet.addHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}
return result;
}
// https校验超时
public static String doHttpsGetCheckTimeOut(String url) throws Exception {
HttpGet httpGet = null;
String result = null;
// 创建Httpclient对象,getCloseableHttpClient-->手工覆盖默认校验规则
// CloseableHttpClient httpClient = getCloseableHttpClient();
CloseableHttpClient httpClient = new SSLClient();
// 使用httpGet方法添加URL
httpGet = new HttpGet(url);
// 设置请求超时时间
RequestConfig config = getRequestConfig();
httpGet.setConfig(config);
// 添加头部请求体
httpGet.addHeader("Content-Type", "application/json");
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}
} catch (SocketTimeoutException | ConnectTimeoutException ex) {
throw new BusiException("", "请求连接超时");
} catch (Exception ex) {
throw new BusiException("", "请求异常,异常信息:" + ex.getMessage());
}
return result;
}
// 设置连接超时
private static RequestConfig getRequestConfig() {
RequestConfig config = RequestConfig.custom().setConnectTimeout(DEF_CONN_TIMEOUT) // 从服务器连接超时时间:httpclient会创建一个异步线程用以创建socket连接,此处设置该socket的连接超时时间
.setSocketTimeout(DEF_READ_TIMEOUT) // socket读数据超时时间:从服务器获取响应数据的超时时间
.setConnectionRequestTimeout(DEF_REQUEST_TIMEOUT) // 从连接池中获取连接超时时间
.build();
return config;
}
// 手工覆盖默认校验规则
private static CloseableHttpClient getCloseableHttpClient() {
CloseableHttpClient client = HttpClients.createDefault();
HttpClientBuilder builder = HttpClients.custom(); // 手工覆盖默认的校验规则
builder.setSSLHostnameVerifier((hostName, sslSession) -> {
return true; // 证书校验通过
});
CloseableHttpClient build = builder.build();
client = build;
return client;
}
public static String doHttpsPostCheckTimeOut(String url, String job) throws Exception {
String result = "";
HttpPost httpPost = new HttpPost(url);
// 创建httpClient,手工覆盖默认的校验规则
// CloseableHttpClient httpClient = getCloseableHttpClient();
CloseableHttpClient httpClient = new SSLClient();
// 设置请求超时时间
RequestConfig config = getRequestConfig();
StringEntity s = new StringEntity(job, "utf-8");
httpPost.setConfig(config);
httpPost.setEntity(s);
System.out.println("请求地址:" + url);
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity, "utf-8");
return result;
}
public static String sendHttpPostCheckTimeOut(HttpPost httpPost) throws Exception {
String result = "";
CloseableHttpClient httpClient = null;
try {
// 创建httpClient,手工覆盖默认的校验规则
httpClient = new SSLClient();
// httpClient = getCloseableHttpClient();
// 设置请求超时时间
RequestConfig config = getRequestConfig();
httpPost.setConfig(config);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity, "utf-8");
} catch (SocketTimeoutException | ConnectTimeoutException ex) {
throw new BusiException("", "请求连接超时");
} catch (Exception ex) {
throw new BusiException("", "请求异常,异常信息:" + ex.getMessage());
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return result;
}
}