作者:是远航呀 | 来源:互联网 | 2023-09-24 13:05
PS:代码是copy腾讯提供的demo,但运行有问题,望大拿能够帮忙解决
加载证书时间出现如下错误:
java.io.IOException: DER input, Integer tag error
at sun.security.util.DerInputStream.getInteger(DerInputStream.java:151)
at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1202)
at java.security.KeyStore.load(KeyStore.java:1183)
at com.caibeike.tps.utils.HttpTookit.doSSLPostP12(HttpTookit.java:86)
at com.caibeike.tps.finance.controller.RefundController.submitTenRefundBatch(RefundController.java:184)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
我的代码:
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(p12FilePath);
char[] pwd = p12Pwd.toCharArray();
try {
keyStore.load(instream, pwd);
} catch(Exception e){
e.printStackTrace();
} finally {
instream.close();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| // Trust own CA and all self-signed certs
SSLContext sslcOntext= SSLContexts.custom().loadKeyMaterial(keyStore, p12Pwd.toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
try {
HttpGet httpget = new HttpGet(urlStr);
System.out.println("executing request" + httpget.getRequestLine());
CloseableHttpResponse respOnse= httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
String text;
while ((text = bufferedReader.readLine()) != null) {
System.out.println(text);
}
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
} |