Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-简单的GET请求,使用SSL证书和HTTPS_Java_Ssl_Https_Request_Certificate - Fatal编程技术网

JAVA-简单的GET请求,使用SSL证书和HTTPS

JAVA-简单的GET请求,使用SSL证书和HTTPS,java,ssl,https,request,certificate,Java,Ssl,Https,Request,Certificate,我有一个扩展名为“.pfx”的文件和此证书的密码 我需要做的是向Web服务发送一个简单的GET请求并读取响应正文 我需要实现与此类似的方法: String getHttpResponse(String url, String certificateFile, String passwordToCertificate){ ... } 我还尝试使用openssl将证书转换为“无密码”格式: Convert a PKCS#12 file (.pfx .p12) containing a pr

我有一个扩展名为“.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和其他东西的一些基本假设方面有问题。

这个问题应该有你的答案:


您需要使用httpclient创建请求,然后使用密钥管理器。

我终于找到了一个好的解决方案(无需创建自定义SSL上下文):


我可以知道您正在使用哪个环境(服务器)吗?该应用程序将在Apache Tomcat上运行。我向其发送请求的服务器是某种返回XML的SOAP Web服务。我尝试了这种方式,但是我不确定应该使用哪个文件作为“信任库”…根据[this](),创建空密钥库可以为null。这在我的情况下很好。但是-如果我需要在运行时更改设置怎么办?您认为再次设置System.setProperty会非常错误吗?这并不是说它会“错误”,而是更可能它不会工作。
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();
}