https请求带证书发送报文

Posted wyllxx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了https请求带证书发送报文相关的知识,希望对你有一定的参考价值。

public String sendXml(String sendurl, String sendData) throws Exception {
File trustedKeystoreFile = new File(sslTrustStore);
BufferedReader reader = null;
CloseableHttpResponse resp = null;
String resultuestStr1 = null;
try {
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(trustedKeystoreFile, sslTrustStorePassword.toCharArray(),
sslTrustStorePassword.toCharArray())
.build();

SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslcontext,
new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" }, null, new NoopHostnameVerifier());

CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
HttpPost post = new HttpPost(sendurl);
HttpEntity entity = new StringEntity(sendData, "utf-8");
post.setHeader("Content-type", "text/xml");
post.setEntity(entity);
resp = client.execute(post);
reader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuilder dataString = new StringBuilder();
String inputLine;
while ((inputLine = reader.readLine()) != null) {
dataString.append(inputLine).append(" ");
}
resultuestStr1 = dataString.toString().replace("&lt;", "<");
} catch (KeyManagementException e1) {
e1.printStackTrace();
} catch (UnrecoverableKeyException e1) {
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (KeyStoreException e1) {
e1.printStackTrace();
} catch (CertificateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return resultuestStr1;
}






































以上是关于https请求带证书发送报文的主要内容,如果未能解决你的问题,请参考以下文章

修改请求和返回报文

C# HttpClient 带证书https 请求

带jsk证书,请求https接口

iOS使用自签名证书实现HTTPS请求

https 忽略 证书 发送请求

用java做一个httpClient 发送https 的get请求,需要证书验证的那种,求大神指点一下!