liujie
2023-08-02 b64d67ef01e81a857046f19dd556b4e4f0695e1c
cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java
@@ -39,7 +39,7 @@
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    private PoolingHttpClientConnectionManager connectionManager;
    private static PoolingHttpClientConnectionManager connectionManager;
    public HttpClientUtil(){
@@ -53,14 +53,14 @@
    /**
     * 创建一个httpClient对象
     */
    private CloseableHttpClient getHttpCline(){
    private static CloseableHttpClient getHttpCline(){
        return  HttpClients.custom()
                .setConnectionManager(connectionManager)
                .disableAutomaticRetries()
                .build();
    }
    private RequestConfig getRequestConfig(){
    private static RequestConfig getRequestConfig(){
        RequestConfig.Builder builder = RequestConfig.custom();
        builder.setSocketTimeout(60000)//3.1设置客户端等待服务端返回数据的超时时间
                .setConnectTimeout(30000)//3.2设置客户端发起TCP连接请求的超时时间
@@ -169,7 +169,7 @@
     * @param header    自定义请求头
     * @return
     */
    public HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
    public static HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(getRequestConfig());
        for(String key : header.keySet()){
@@ -181,7 +181,7 @@
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        HttpResult httpResult = HttpResult.getHttpResult(statusCode, content);
        this.close(httpResponse);
        close(httpResponse);
        return httpResult;
    }
@@ -198,14 +198,14 @@
     * @return
     * @throws Exception
     */
    public String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{
    public static String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{
        HttpPost httpPost = new HttpPost(url);
        for(String key : header.keySet()){
            httpPost.setHeader(key, header.get(key));
        }
        httpPost.setHeader("Content-Type", "application/xml");
        httpPost.setEntity(new StringEntity(xml, "UTF-8"));
        CloseableHttpClient httpCline = this.initCert(certPassword, certPath, certType);
        CloseableHttpClient httpCline = initCert(certPassword, certPath, certType);
        CloseableHttpResponse httpResponse = httpCline.execute(httpPost);
        String content = null;
        if(httpResponse.getStatusLine().getStatusCode() == 200){
@@ -213,7 +213,7 @@
        }else{
            content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity());
        }
        this.close(httpResponse);
        close(httpResponse);
        httpCline.close();
        return content;
    }
@@ -226,7 +226,7 @@
     * @param certType  证书类型
     * @throws Exception
     */
    private CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
    private static CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(certType);
        InputStream inputStream = new FileInputStream(new File(certPath));
        try {
@@ -246,7 +246,7 @@
    /**
     * 关闭资源
     */
    private void close(CloseableHttpResponse httpResponse){
    private static void close(CloseableHttpResponse httpResponse){
        try {
            if(null != httpResponse){
                EntityUtils.consume(httpResponse.getEntity());//此处高能,通过源码分析,由EntityUtils是否回收HttpEntity