码迷,mamicode.com
                                                                              首页 > 微信 > 详细

                                                                              微信支付 带apiclient_cert.p12证书的请求方法 JAVA版

                                                                              时间:2018-05-12 14:24:18      阅读:1367      评论:0      收藏:0      [点我收藏+]

                                                                              标签:instance   sea   默认   head   exception   tostring   ons   微信   warning   

                                                                              以下是带apiclient_cert.p12证书的请求方法

                                                                              package utils.wechat;
                                                                              
                                                                              import java.io.File;
                                                                              import java.io.FileInputStream;
                                                                              import java.io.IOException;
                                                                              import java.security.KeyStore;
                                                                              
                                                                              import javax.net.ssl.SSLContext;
                                                                              
                                                                              import org.apache.http.HttpEntity;
                                                                              import org.apache.http.HttpResponse;
                                                                              import org.apache.http.client.config.RequestConfig;
                                                                              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;
                                                                              
                                                                              /**
                                                                               * 获取微信apiclient_cert.p12证书
                                                                               * 
                                                                               * @author zengwei
                                                                               * @email [email protected]
                                                                               * @date 2018年4月5日 下午4:49:36
                                                                               */
                                                                              @SuppressWarnings("deprecation")
                                                                              public class CertHttpUtil {
                                                                              
                                                                                  private static int socketTimeout = 10000;// 连接超时时间,默认10秒
                                                                                  private static int connectTimeout = 30000;// 传输超时时间,默认30秒
                                                                                  private static RequestConfig requestConfig;// 请求器的配置
                                                                                  private static CloseableHttpClient httpClient;// HTTP请求器
                                                                              
                                                                                  /**
                                                                                   * 通过Https往API post xml数据
                                                                                   * 
                                                                                   * @param url API地址
                                                                                   * @param xmlObj 要提交的XML数据对象
                                                                                  * @param mchId 商户ID
                                                                                  * @param certPath 证书位置
                                                                                   * @return
                                                                                   */
                                                                                  public static String postData(String url, String xmlObj, String mchId, String certPath) {
                                                                                      // 加载证书
                                                                                      try {
                                                                                          initCert(mchId, certPath);
                                                                                      } catch (Exception e) {
                                                                                          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);
                                                                                      // 根据默认超时限制初始化requestConfig
                                                                                      requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
                                                                                      // 设置请求器的配置
                                                                                      httpPost.setConfig(requestConfig);
                                                                                      try {
                                                                                          HttpResponse response = null;
                                                                                          try {
                                                                                              response = httpClient.execute(httpPost);
                                                                                          } catch (IOException e) {
                                                                                              e.printStackTrace();
                                                                                          }
                                                                                          HttpEntity entity = response.getEntity();
                                                                                          try {
                                                                                              result = EntityUtils.toString(entity, "UTF-8");
                                                                                          } catch (IOException e) {
                                                                                              e.printStackTrace();
                                                                                          }
                                                                                      } finally {
                                                                                          httpPost.abort();
                                                                                      }
                                                                                      return result;
                                                                                  }
                                                                              
                                                                                  /**
                                                                                   * 加载证书
                                                                                   * 
                                                                                   * @param mchId 商户ID
                                                                                   * @param certPath 证书位置
                                                                                   * @throws Exception
                                                                                   */
                                                                                  private static void initCert(String mchId, String certPath) throws Exception {
                                                                                      // 证书密码,默认为商户ID
                                                                                      String key = mchId;
                                                                                      // 证书的路径
                                                                                      String path = certPath;
                                                                                      // ?#20184;?#35835;取证书格式为PKCS12
                                                                                      KeyStore keyStore = KeyStore.getInstance("PKCS12");
                                                                                      // 读取本机存放的PKCS12证书文件
                                                                                      FileInputStream instream = new FileInputStream(new File(path));
                                                                                      try {
                                                                                          // ?#20184;≒KCS12的密码(商户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();
                                                                                  }
                                                                              }
                                                                              

                                                                                

                                                                              微信支付 带apiclient_cert.p12证书的请求方法 JAVA版

                                                                              标签:instance   sea   默认   head   exception   tostring   ons   微信   warning   

                                                                              原文地址:https://www.cnblogs.com/zengweiB208/p/9025545.html

                                                                              (0)
                                                                              (0)
                                                                                 
                                                                              举报
                                                                              评论 一句话评论(0
                                                                              0条  
                                                                              登录后才能评论!
                                                                              ? 2014 mamicode.com 版权所有 京ICP备13008772号-2
                                                                              迷上了代码!
                                                                              宁夏11选5开奖查询