[英]SSL Certificate Issue for HTTPS GET request and not for HTTPS POST request
[英]JAVA - Simple GET request, using SSL certificate and HTTPS
我有一個帶有'.pfx'擴展名的文件和這個證書的密碼。
我需要做的是向web服務發送一個簡單的GET請求並閱讀響應主體。
我需要實現一個類似於此的方法:
String getHttpResponse(String url, String certificateFile, String passwordToCertificate){
...
}
我還嘗試使用openssl將證書轉換為“無密碼”格式:
Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM:
openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes
所以我的方法的替代實現可能是:
String getHttpResponse(String url, String certificateFile){
...
}
我真的很感謝你的幫助,我花了半天時間用谷歌搜索它,但我還沒有找到一個可以幫助我的例子,似乎我有一些關於SSL和東西的一些基本假設的問題。
我終於找到了一個很好的解決方案(不創建自定義SSL上下文):
String getHttpResponseWithSSL(String url) throws Exception {
//default truststore parameters
System.setProperty("javax.net.ssl.trustStore", "/usr/lib/jvm/java-6-openjdk/jre/lib/securitycacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
//my certificate and password
System.setProperty("javax.net.ssl.keyStore", "mycert.pfx");
System.setProperty("javax.net.ssl.keyStorePassword", "mypass");
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
HttpClient httpclient = new HttpClient();
GetMethod method = new GetMethod();
method.setPath(url);
int statusCode = httpclient.executeMethod(method);
System.out.println("Status: " + statusCode);
method.releaseConnection();
return method.getResponseBodyAsString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.