From 3cb61867769d091f7e20ade8701b6593b82141ad Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期四, 26 九月 2024 14:40:10 +0800 Subject: [PATCH] 玩湃微信商户分账 --- cloud-server-course/src/main/java/com/dsh/course/util/httpClinet/HttpClientUtil.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) diff --git a/cloud-server-course/src/main/java/com/dsh/course/util/httpClinet/HttpClientUtil.java b/cloud-server-course/src/main/java/com/dsh/course/util/httpClinet/HttpClientUtil.java index d4b0bfe..0e969e4 100644 --- a/cloud-server-course/src/main/java/com/dsh/course/util/httpClinet/HttpClientUtil.java +++ b/cloud-server-course/src/main/java/com/dsh/course/util/httpClinet/HttpClientUtil.java @@ -242,6 +242,61 @@ SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } + /** + * 请求https发送XML请求 + * + * @param url 接口路径 + * @param xml 内容 + * @param header 请求头 + * @param certPassword 证书密码 + * @param certPath 证书路径 + * @param certType 证书类型 + * @return + * @throws Exception + */ + public static String pushHttpsRequsetXml1(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception { + HttpPost httpPost = new HttpPost(url); + for (String key : header.keySet()) { + httpPost.setHeader(key, header.get(key)); + } + httpPost.setHeader("Content-Type", "application/xml"); + httpPost.setEntity(new StringEntity(xml, "UTF-8")); + CloseableHttpClient httpCline = initCert1(certPassword, certPath, certType); + CloseableHttpResponse httpResponse = httpCline.execute(httpPost); + String content = null; + if (httpResponse.getStatusLine().getStatusCode() == 200) { + content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); + } else { + content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); + } + close(httpResponse); + httpCline.close(); + return content; + } + + + /** + * 初始化https对象(带证书) + * + * @param key 证书密码 + * @param certPath 证书路径 + * @param certType 证书类型 + * @throws Exception + */ + private static CloseableHttpClient initCert1(String key, String certPath, String certType) throws Exception { + KeyStore keyStore = KeyStore.getInstance(certType); + InputStream inputStream = new FileInputStream(new File(certPath)); + try { + keyStore.load(inputStream, key.toCharArray()); + } finally { + inputStream.close(); + } + SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build(); + SSLConnectionSocketFactory sslsf = + new SSLConnectionSocketFactory(sslcontext, null, null, + SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } /** -- Gitblit v1.7.1