You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

183 lines
6.9 KiB

package com.fr.plugin.third.party.jsdbacfa;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.general.IOUtils;
import com.fr.third.org.apache.http.HttpEntity;
import com.fr.third.org.apache.http.HttpStatus;
import com.fr.third.org.apache.http.client.config.RequestConfig;
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
import com.fr.third.org.apache.http.client.methods.HttpGet;
import com.fr.third.org.apache.http.client.methods.HttpPost;
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier;
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fr.third.org.apache.http.entity.StringEntity;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.ssl.SSLContextBuilder;
import com.fr.third.org.apache.http.ssl.TrustStrategy;
import com.fr.third.org.apache.http.util.EntityUtils;
import com.fr.third.springframework.web.util.UriUtils;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class Utils {
public static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36";
public static RequestConfig REQUEST_CONFIG = RequestConfig.custom()
.setConnectionRequestTimeout(30000)
.setSocketTimeout(30000) // 服务端相应超时
.setConnectTimeout(30000) // 建立socket链接超时时间
.build();
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
}
return HttpClients.createDefault();
}
public static synchronized CloseableHttpClient createHttpClient(String url) {
CloseableHttpClient httpClient = null;
if (StringKit.isEmpty(url)) {
httpClient = HttpClients.createDefault();
return httpClient;
}
if (url.startsWith("https://")) {
httpClient = createSSLClientDefault();
return httpClient;
}
httpClient = HttpClients.createDefault();
return httpClient;
}
public static synchronized String createHttpGetContent(CloseableHttpClient httpClient, String url) throws IOException {
if ((httpClient == null) || (StringKit.isEmpty(url))) {
return "";
}
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT);
httpGet.setConfig(Utils.REQUEST_CONFIG);
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
response.close();
LogKit.info("http请求出错,http status:" + statusCode);
return "";
}
HttpEntity httpEntity = response.getEntity();
if (httpEntity == null) {
response.close();
LogKit.info("http请求出错,http响应内容为空");
return "";
}
String responseContent = EntityUtils.toString(httpEntity, "UTF-8");
response.close();
if (StringKit.isEmpty(responseContent)) {
LogKit.info("http请求出错,http响应内容为空1");
return "";
}
return responseContent;
}
public static synchronized String createHttpPostContent(CloseableHttpClient httpClient, String url, String bodyContent) throws IOException {
if ((httpClient == null) || (StringKit.isEmpty(url)) || (StringKit.isEmpty(bodyContent))) {
return "";
}
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT);
httpPost.setConfig(Utils.REQUEST_CONFIG);
StringEntity bodyEntity = new StringEntity(bodyContent, "UTF-8");
httpPost.setEntity(bodyEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
response.close();
LogKit.info("http请求出错,http status:" + statusCode);
return "";
}
HttpEntity httpEntity = response.getEntity();
if (httpEntity == null) {
response.close();
LogKit.info("http请求出错,http响应内容为空");
return "";
}
String responseContent = EntityUtils.toString(httpEntity, "UTF-8");
response.close();
if (StringKit.isEmpty(responseContent)) {
LogKit.info("http请求出错,http响应内容为空1");
return "";
}
return responseContent;
}
/**
* 获取请求主体内容
* @param req
* @return
* @throws IOException
*/
public static String getHttpRequestBody(HttpServletRequest req) throws IOException {
if (req == null) {
return "";
}
ServletInputStream inputStream = req.getInputStream();
if (inputStream == null) {
return "";
}
String content = IOUtils.inputStream2String(inputStream);
if (StringKit.isEmpty(content)) {
return "";
}
return content;
}
/**
* 用utf-8按url规则编码
*
* @param value
* @return
* @throws URISyntaxException
*/
public static String encodeUrlWithUtf8(String value) {
if ((value == null) || (value.length() <= 0)) {
return "";
}
/*String path = "/" + value;
URI uri = new URI("http", "a", path, null);
String tempValue = uri.toASCIIString();
String encodedValue = tempValue.substring(9);*/
String tempValue = "";
try {
tempValue = UriUtils.encodeQueryParam(value, "UTF-8");
return tempValue;
} catch (Exception e) {
LogKit.error("Utils.encodeUrlWithUtf8:" + e.getMessage(), e);
tempValue = value;
}
return tempValue;
}
}