chenjiahe
2023-12-04 d9fe4b6ff40a964c895a452ea12a04a13a979ec4
提交 | 用户 | age
5c5945 1 package com.hx.mp.util;
E 2
3 import org.apache.http.client.methods.CloseableHttpResponse;
4 import org.apache.http.client.methods.HttpPost;
5 import org.apache.http.entity.StringEntity;
6 import org.apache.http.impl.client.HttpClients;
7
8 public class HttpUtil {
9     /**
10      * 发送post请求
11      * 
12      * @param url
13      *            请求地址
14      * @param outputEntity
15      *            发送内容
16      * @param isLoadCert
17      *            是否加载证书
18      */
19     public static CloseableHttpResponse Post(String url, String outputEntity, boolean isLoadCert, String certPath, String mchId) throws Exception {
20         HttpPost httpPost = new HttpPost(url);
21         // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
22         httpPost.addHeader("Content-Type", "text/xml");
23         httpPost.setEntity(new StringEntity(outputEntity, "UTF-8"));
24         if (isLoadCert) {
25             // 加载含有证书的http请求
26             return HttpClients.custom().setSSLSocketFactory(CertUtil.initCert(certPath, mchId)).build().execute(httpPost);
27         } else {
28             return HttpClients.custom().build().execute(httpPost);
29         }
30     }
31 }