|
|
@ -4,8 +4,6 @@ import com.alibaba.fastjson.JSON; |
|
|
|
import com.epmet.commons.tools.dto.form.DingTalkTextMsg; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.scan.param.TextScanParamDTO; |
|
|
|
import com.epmet.commons.tools.scan.param.TextTaskDTO; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
|
import org.apache.commons.lang3.ArrayUtils; |
|
|
@ -20,22 +18,34 @@ import org.apache.http.client.methods.HttpGet; |
|
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
|
import org.apache.http.client.methods.HttpRequestBase; |
|
|
|
import org.apache.http.client.utils.URIBuilder; |
|
|
|
import org.apache.http.config.Registry; |
|
|
|
import org.apache.http.config.RegistryBuilder; |
|
|
|
import org.apache.http.conn.socket.ConnectionSocketFactory; |
|
|
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder; |
|
|
|
import org.apache.http.entity.mime.content.FileBody; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
|
|
|
import org.apache.http.message.BasicNameValuePair; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import javax.crypto.Mac; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
import javax.net.ssl.TrustManager; |
|
|
|
import javax.net.ssl.X509TrustManager; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.security.KeyManagementException; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.security.cert.CertificateException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
@ -55,6 +65,28 @@ public class HttpClientManager { |
|
|
|
* HttpClient对象 |
|
|
|
*/ |
|
|
|
private static CloseableHttpClient httpclient = HttpClients.custom().disableAutomaticRetries().build(); |
|
|
|
private static CloseableHttpClient httpsClient; |
|
|
|
|
|
|
|
static { |
|
|
|
try { |
|
|
|
// 采用绕过验证的方式处理https请求
|
|
|
|
SSLContext sslcontext = createIgnoreVerifySSL(); |
|
|
|
// 设置协议http和https对应的处理socket链接工厂的对象
|
|
|
|
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() |
|
|
|
.register("http", PlainConnectionSocketFactory.INSTANCE) |
|
|
|
.register("https", new SSLConnectionSocketFactory(sslcontext, |
|
|
|
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)) |
|
|
|
.build(); |
|
|
|
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( |
|
|
|
socketFactoryRegistry); |
|
|
|
HttpClients.custom().setConnectionManager(connManager); |
|
|
|
|
|
|
|
// 创建自定义的httpclient对象
|
|
|
|
httpsClient = HttpClients.custom().setConnectionManager(connManager).build(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*** 超时设置 ****/ |
|
|
|
private static RequestConfig requestConfig = RequestConfig.custom() |
|
|
@ -78,7 +110,7 @@ public class HttpClientManager { |
|
|
|
* |
|
|
|
* @author: jianjun liu |
|
|
|
*/ |
|
|
|
public Result<String> sendPost(String url, Map<String, String> paramsMap) { |
|
|
|
public Result<String> sendPost(String url, Map<String, Object> paramsMap) { |
|
|
|
|
|
|
|
try { |
|
|
|
HttpPost httppost = new HttpPost(url); |
|
|
@ -92,9 +124,8 @@ public class HttpClientManager { |
|
|
|
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8"); |
|
|
|
httppost.setEntity(urlEncodedFormEntity); |
|
|
|
|
|
|
|
return execute(httppost); |
|
|
|
return execute(httppost, false); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("send exception", e); |
|
|
|
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
@ -119,7 +150,7 @@ public class HttpClientManager { |
|
|
|
StringEntity se = new StringEntity(jsonStrParam, "utf-8"); |
|
|
|
httppost.setEntity(se); |
|
|
|
} |
|
|
|
return execute(httppost); |
|
|
|
return execute(httppost, false); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("send exception", e); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
@ -144,13 +175,38 @@ public class HttpClientManager { |
|
|
|
HttpEntity reqEntity = MultipartEntityBuilder.create() |
|
|
|
.addPart("media", fileBody).build(); |
|
|
|
httppost.setEntity(reqEntity); |
|
|
|
return execute(httppost); |
|
|
|
return execute(httppost,false); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("send exception", e); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc: https发送json post 请求 |
|
|
|
* param: url,jsonStrParam |
|
|
|
* return: Result<String> |
|
|
|
* date: 2019/2/21 9:12 |
|
|
|
* |
|
|
|
* @author: jianjun liu |
|
|
|
*/ |
|
|
|
public Result<String> sendPostByHttps(String url, String jsonStrParam) { |
|
|
|
try { |
|
|
|
HttpPost httppost = new HttpPost(url); |
|
|
|
httppost.setConfig(requestConfig); |
|
|
|
httppost.addHeader("Content-Type", "application/json"); |
|
|
|
if (StringUtils.isNotEmpty(jsonStrParam)) { |
|
|
|
StringEntity se = new StringEntity(jsonStrParam, "utf-8"); |
|
|
|
httppost.setEntity(se); |
|
|
|
} |
|
|
|
return execute(httppost, true); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("sendPostByHttps exception", e); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc: 发送钉钉群消息 简版 |
|
|
|
* param: url,jsonStrParam |
|
|
@ -168,10 +224,10 @@ public class HttpClientManager { |
|
|
|
Mac mac = Mac.getInstance("HmacSHA256"); |
|
|
|
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); |
|
|
|
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); |
|
|
|
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8"); |
|
|
|
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); |
|
|
|
DingTalkTextMsg msg = new DingTalkTextMsg(); |
|
|
|
msg.setContent(content); |
|
|
|
url = url.concat("×tamp="+timestamp+"&sign="+sign); |
|
|
|
url = url.concat("×tamp=" + timestamp + "&sign=" + sign); |
|
|
|
String jsonStrParam = msg.getMsgContent(); |
|
|
|
return sendPostByJSON(url, jsonStrParam); |
|
|
|
} catch (Exception e) { |
|
|
@ -200,25 +256,28 @@ public class HttpClientManager { |
|
|
|
} |
|
|
|
HttpGet httpGet = new HttpGet(builder.build()); |
|
|
|
httpGet.setConfig(requestConfig); |
|
|
|
return execute(httpGet); |
|
|
|
return execute(httpGet, false); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("sendGet exception", e); |
|
|
|
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Result<String> execute(HttpRequestBase httpMethod) { |
|
|
|
private Result<String> execute(HttpRequestBase httpMethod, boolean isHttps) { |
|
|
|
CloseableHttpResponse response = null; |
|
|
|
try { |
|
|
|
response = httpclient.execute(httpMethod); |
|
|
|
log.debug("http send response:{}", JSON.toJSONString(response)); |
|
|
|
if (isHttps) { |
|
|
|
response = httpsClient.execute(httpMethod); |
|
|
|
} else { |
|
|
|
response = httpclient.execute(httpMethod); |
|
|
|
} |
|
|
|
if (response != null && response.getStatusLine() != null) { |
|
|
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
|
|
|
String result = EntityUtils.toString(response.getEntity()); |
|
|
|
return new Result<String>().ok(result); |
|
|
|
} else { |
|
|
|
log.warn("execute http method fail,httpStatus:{}", response.getStatusLine().getStatusCode()); |
|
|
|
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(),"请求失败httpStatus:"+response.getStatusLine().getStatusCode()); |
|
|
|
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), "请求失败httpStatus:" + response.getStatusLine().getStatusCode()); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
@ -297,17 +356,29 @@ public class HttpClientManager { |
|
|
|
return new Result<Byte[]>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
String url = "http://localhost:8107/epmetscan/api/textSyncScan"; |
|
|
|
TextTaskDTO p = new TextTaskDTO(); |
|
|
|
p.setDataId("1"); |
|
|
|
p.setContent("neirong1"); |
|
|
|
List<TextTaskDTO> list = new ArrayList<>(); |
|
|
|
list.add(p); |
|
|
|
TextScanParamDTO param = new TextScanParamDTO(); |
|
|
|
param.setTasks(list); |
|
|
|
Result<String> result = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(param)); |
|
|
|
System.out.println(JSON.toJSONString(result)); |
|
|
|
private static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { |
|
|
|
SSLContext sc = SSLContext.getInstance("SSLv3"); |
|
|
|
|
|
|
|
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
|
X509TrustManager trustManager = new X509TrustManager() { |
|
|
|
@Override |
|
|
|
public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, |
|
|
|
String paramString) throws CertificateException { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, |
|
|
|
String paramString) throws CertificateException { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
|
|
|
return null; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
sc.init(null, new TrustManager[]{trustManager}, null); |
|
|
|
return sc; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|