微信支付--工具类--通过Https往API post xml数据

  1. import org.apache.http.HttpEntity;  
  2. import org.apache.http.HttpResponse;  
  3. import org.apache.http.client.config.RequestConfig;  
  4. import org.apache.http.client.methods.HttpPost;  
  5. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  
  6. import org.apache.http.conn.ssl.SSLContexts;  
  7. import org.apache.http.entity.StringEntity;  
  8. import org.apache.http.impl.client.CloseableHttpClient;  
  9. import org.apache.http.impl.client.HttpClients;  
  10. import org.apache.http.util.EntityUtils;  
[java]  view plain copy
  1. import javax.net.ssl.SSLContext;  
  2. import java.io.IOException;  
  3. import java.security.*;  
  4.   
  5. public class HttpsRequest {  
  6.   
  7.     public interface ResultListener {  
  8.   
  9.         public void onConnectionPoolTimeoutError();  
  10.   
  11.     }  
  12.   
  13.     // 表示请求器是否已经做了初始化工作  
  14.     private boolean hasInit = false;  
  15.   
  16.     // 连接超时时间,默认10秒  
  17.     private int socketTimeout = 10000;  
  18.   
  19.     // 传输超时时间,默认30秒  
  20.     private int connectTimeout = 30000;  
  21.   
  22.     // 请求器的配置  
  23.     private RequestConfig requestConfig;  
  24.   
  25.     // HTTP请求器  
  26.     private CloseableHttpClient httpClient;  
  27.   
  28.     public HttpsRequest() throws UnrecoverableKeyException,  
  29.             KeyManagementException, NoSuchAlgorithmException,  
  30.             KeyStoreException, IOException {  
  31.         init();  
  32.     }  
  33.   
  34.     private void init() throws IOException, KeyStoreException,  
  35.             UnrecoverableKeyException, NoSuchAlgorithmException,  
  36.             KeyManagementException {  
  37.   
  38.         // Trust own CA and all self-signed certs  
  39.         SSLContext sslcontext = SSLContexts.custom().build();  
  40.         // Allow TLSv1 protocol only  
  41.         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(  
  42.                 sslcontext, new String[] { "TLSv1" }, null,  
  43.                 SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  
  44.   
  45.         httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();  
  46.   
  47.         // 根据默认超时限制初始化requestConfig  
  48.         requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)  
  49.                 .setConnectTimeout(connectTimeout).build();  
  50.   
  51.         hasInit = true;  
  52.     }  
  53.   
  54.     /** 
  55.      * 通过Https往API post xml数据 
  56.      *  
  57.      * @param url 
  58.      *            API地址 
  59.      * @param xmlObj 
  60.      *            要提交的XML数据对象 
  61.      * @return API回包的实际数据 
  62.      * @throws IOException 
  63.      * @throws KeyStoreException 
  64.      * @throws UnrecoverableKeyException 
  65.      * @throws NoSuchAlgorithmException 
  66.      * @throws KeyManagementException 
  67.      */  
  68.   
  69.     public String sendPost(String url, String postDataXML) throws IOException,  
  70.             KeyStoreException, UnrecoverableKeyException,  
  71.             NoSuchAlgorithmException, KeyManagementException {  
  72.   
  73.         if (!hasInit) {  
  74.             init();  
  75.         }  
  76.   
  77.         String result = null;  
  78.   
  79.         HttpPost httpPost = new HttpPost(url);  
  80.   
  81.         // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别  
  82.         StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");  
  83.         httpPost.addHeader("Content-Type""text/xml");  
  84.         httpPost.setEntity(postEntity);  
  85.   
  86.         // 设置请求器的配置  
  87.         httpPost.setConfig(requestConfig);  
  88.   
  89.         try {  
  90.             HttpResponse response = httpClient.execute(httpPost);  
  91.   
  92.             HttpEntity entity = response.getEntity();  
  93.   
  94.             result = EntityUtils.toString(entity, "UTF-8");  
  95.   
  96.         } catch (Exception e) {  
  97.             e.printStackTrace();  
  98.   
  99.         } finally {  
  100.             httpPost.abort();  
  101.         }  
  102.   
  103.         return result;  
  104.     }  
  105.   
  106.     /** 
  107.      * 设置连接超时时间 
  108.      *  
  109.      * @param socketTimeout 
  110.      *            连接时长,默认10秒 
  111.      */  
  112.     public void setSocketTimeout(int socketTimeout) {  
  113.         this.socketTimeout = socketTimeout;  
  114.         resetRequestConfig();  
  115.     }  
  116.   
  117.     /** 
  118.      * 设置传输超时时间 
  119.      *  
  120.      * @param connectTimeout 
  121.      *            传输时长,默认30秒 
  122.      */  
  123.     public void setConnectTimeout(int connectTimeout) {  
  124.         this.connectTimeout = connectTimeout;  
  125.         resetRequestConfig();  
  126.     }  
  127.   
  128.     private void resetRequestConfig() {  
  129.         requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)  
  130.                 .setConnectTimeout(connectTimeout).build();  
  131.     }  
  132.   
  133.     /** 
  134.      * 允许商户自己做更高级更复杂的请求器配置 
  135.      *  
  136.      * @param requestConfig 
  137.      *            设置HttpsRequest的请求器配置 
  138.      */  
  139.     public void setRequestConfig(RequestConfig requestConfig) {  
  140.         this.requestConfig = requestConfig;  
  141.     }  
  142. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值