dst*_*zak 4 java ssl https certificate request
我有一个带有'.pfx'扩展名的文件和这个证书的密码.
我需要做的是向web服务发送一个简单的GET请求并阅读响应主体.
我需要实现一个类似于此的方法:
String getHttpResponse(String url, String certificateFile, String passwordToCertificate){
...
}
Run Code Online (Sandbox Code Playgroud)
我还尝试使用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
Run Code Online (Sandbox Code Playgroud)
所以我的方法的替代实现可能是:
String getHttpResponse(String url, String certificateFile){
...
}
Run Code Online (Sandbox Code Playgroud)
我会很感激你的帮助,我花了半天google搜索,但我还没有找到一个例子,这将有助于我,似乎我有undestanding周围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();
}
Run Code Online (Sandbox Code Playgroud)