JAVA - Simple GET request, using SSL certificate and HTTPS

前端 未结 2 1171
野的像风
野的像风 2020-12-30 17:27

I have a file with the \'.pfx\' extension and a password to this certificate.

What I need to do is to send a simple GET request to a webservice and read the response

相关标签:
2条回答
  • 2020-12-30 18:13

    This question should have your answer:

    HTTPClient-1.4.2: Explanation needed for Custom SSL Context Example

    You need to use httpclient to create the request and then use a key manager.

    0 讨论(0)
  • 2020-12-30 18:22

    I finally found a good solution (without creating custom SSL context):

    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();
    }
    
    0 讨论(0)
提交回复
热议问题