| package com.xzx.gc.util.pay.util;  | 
|   | 
| import com.xzx.gc.util.pay.util.entity.TransfersDtoAdmin;  | 
| import org.apache.http.HttpEntity;  | 
| import org.apache.http.HttpResponse;  | 
| import org.apache.http.client.methods.HttpPost;  | 
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  | 
| import org.apache.http.conn.ssl.SSLContexts;  | 
| import org.apache.http.entity.StringEntity;  | 
| import org.apache.http.impl.client.CloseableHttpClient;  | 
| import org.apache.http.impl.client.HttpClients;  | 
| import org.apache.http.util.EntityUtils;  | 
|   | 
| import javax.net.ssl.SSLContext;  | 
| import java.io.File;  | 
| import java.io.FileInputStream;  | 
| import java.io.IOException;  | 
| import java.security.*;  | 
|   | 
| public class HttpRequestHandler  | 
| {  | 
|   | 
|     // HTTP请求器  | 
|     private static CloseableHttpClient httpClient;  | 
|   | 
|     /**  | 
|      * 加载证书  | 
|      *  | 
|      * @param mchId  | 
|      * @param certPath  | 
|      * @throws IOException  | 
|      * @throws KeyStoreException  | 
|      * @throws UnrecoverableKeyException  | 
|      * @throws NoSuchAlgorithmException  | 
|      * @throws KeyManagementException  | 
|      */  | 
|     private static void initCert(String mchId, String certPath) throws Exception {  | 
|         // 证书密码,默认为商户ID  | 
|         String key = mchId;  | 
|         // 证书的路径  | 
|         String path = certPath;  | 
|         // 指定读取证书格式为PKCS12  | 
|         KeyStore keyStore = KeyStore.getInstance("PKCS12");  | 
|         // 读取本机存放的PKCS12证书文件  | 
|         FileInputStream instream = new FileInputStream(new File(path));  | 
|         try {  | 
|             // 指定PKCS12的密码(商户ID)  | 
|             keyStore.load(instream, key.toCharArray());  | 
|         } finally {  | 
|             instream.close();  | 
|         }  | 
|         SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();  | 
|         SSLConnectionSocketFactory sslsf =  | 
|                 new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,  | 
|                         SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  | 
|         httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();  | 
|     }  | 
|   | 
|     /**  | 
|      * 通过Https往API post xml数据  | 
|      *  | 
|      * @param url  | 
|      *            API地址  | 
|      * @param xmlObj  | 
|      *            要提交的XML数据对象  | 
|      * @param path  | 
|      *            当前目录,用于加载证书  | 
|      * @return  | 
|      * @throws IOException  | 
|      * @throws KeyStoreException  | 
|      * @throws UnrecoverableKeyException  | 
|      * @throws NoSuchAlgorithmException  | 
|      * @throws KeyManagementException  | 
|      */  | 
|     public static String httpsRequest(String url, String xmlObj, TransfersDtoAdmin model, String path)  | 
|     {  | 
|         // 加载证书  | 
|         try {  | 
|             initCert(model.getMchid(), path);  | 
|         } catch (Exception e) {  | 
|             System.out.println("加载证书失败!");  | 
|             e.printStackTrace();  | 
|         }  | 
|         String result = null;  | 
|         HttpPost httpPost = new HttpPost(url);  | 
|         // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别  | 
|         StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");  | 
|         httpPost.addHeader("Content-Type", "text/xml");  | 
|         httpPost.setEntity(postEntity);  | 
|         try  | 
|         {  | 
|             HttpResponse response = httpClient.execute(httpPost);  | 
|             HttpEntity entity = response.getEntity();  | 
|             result = EntityUtils.toString(entity, "UTF-8");  | 
|         }  | 
|         catch (Exception e)  | 
|         {  | 
|             e.printStackTrace();  | 
|         }  | 
|         finally  | 
|         {  | 
|             httpPost.abort();  | 
|         }  | 
|   | 
|         return result;  | 
|     }  | 
| }  |