package com.ssj.framework.core.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** 
 * @author  ZhangCaibao 
 * @version 2018年7月3日 上午10:49:29 
 * 类说明 
 */
public class HttpClientUtil {

    private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class);

    public static String sendSSLPostRequest(String url, String message) {
        return sendSSLPostRequest(url, 40000, message);
    }

    public static String sendSSLPostRequest(String url, int timeout, String message) {

        log.info("input message:{}", message);
        // 响应内容
        String responseContent = null;
        // 这是超时时间
        final HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
        // 创建默认的httpClient实例
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        try {
            // 创建HttpPost
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/xml");
            if (StringUtil.isNotEmpty(message)) {
                StringEntity se = new StringEntity(message, "UTF-8");
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/xml"));
                httpPost.setEntity(se);
            }
            // 执行POST请求
            HttpResponse response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                responseContent = EntityUtils.toString(entity, "UTF-8");
                log.info("responseContent:{}", responseContent);
                EntityUtils.consume(entity);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            httpClient.getConnectionManager().shutdown();
        }
        return responseContent;
    }

    public static String sendBankPostRequest(String url, String message) {
        return sendBankPostRequest(url, 40000, message);
    }

    public static String sendBankPostRequest(String url, int timeout, String message) {

        log.info("input message:{}", message);
        // 响应内容
        String responseContent = null;
        // 这是超时时间
        final HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
        // 创建默认的httpClient实例
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        try {
            // 创建HttpPost
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/xml");
            StringEntity se = new StringEntity(message, "GBK");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/xml"));
            httpPost.setEntity(se);
            // 执行POST请求
            HttpResponse response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                responseContent = EntityUtils.toString(entity, "GBK");
                log.info("responseContent:{}", responseContent);
                EntityUtils.consume(entity);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            httpClient.getConnectionManager().shutdown();
        }
        return responseContent;
    }

   
}
