使用带有客户端认证的HTTP客户端发送SOAP请求时连接超时异常

3

我正在尝试使用客户端证书访问一个url,已经生成了密钥:

keytool -genkey -alias server -keyalg RSA -keystore /example.jks -validity 10950

使用以下内容创建密钥库:

keytool -import -trustcacerts -alias root -file /example.cer -keystore /example.jks

并且尝试连接:

System.out.println("------------------------------------------- In         SendRequest ------------------------------------################");
SSLContext context = SSLContext.getInstance("TLS");
Certificate cert=getCertificate();
URL url = new URL("url");
URLConnection urlConnection = url.openConnection();
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
SSLSocketFactory sslSocketFactory = getFactory();
httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
DataOutputStream wr = new         DataOutputStream(httpsUrlConnection.getOutputStream());
System.out.println(wr.toString());
File req_xml = new File("request.xml");
//SOAPMessage req = TestCase.createSoapSubsribeRequest("SUBSCRIBE");
HttpPost post = new HttpPost("url");
post.setEntity(new InputStreamEntity(new FileInputStream(req_xml), req_xml.length()));
post.setHeader("Content-type", "text/xml; charset=UTF-8");
//post.setHeader("SOAPAction", "");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);

LOG.info("************************************************************RESPONSE****************"+response.getStatusLine());
// SOAP response(xml) get        String res_xml = EntityUtils.toString(response.getEntity());
    LOG.info("Response"+res_xml);
}
private SSLSocketFactory getFactory( )  {
    try{ 
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        System.out.println("------------------------------------------- In getFactory ------------------------------------################");
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        //InputStream keyInput = new FileInputStream(pKeyFile);
        String password = "obsmesh";
                    char[] passwd = password.toCharArray(example.jks");
        keystore.load(is, passwd);
        // keyInput.close();
        keyManagerFactory.init(keystore, password.toCharArray());
        System.out.println("------------------------------------------- In jsdkl ------------------------------------################");
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManager[] trust = null;
        context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
        return context.getSocketFactory();
}catch(Exception e){
    System.out.println(e);
}
return null;

}

1个回答

3

尝试使用以下代码,希望能对您有所帮助。

     KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom()
            .loadTrustMaterial(new File("//your jks file path "), "//key password here",
                    new TrustSelfSignedStrategy())
            .build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslcontext,
            new String[] { "TLSv1" },
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    CloseableHttpClient httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .build();
    try {


     File req_xml = new File("// your request xml file path");


   HttpPost post = new HttpPost("//https client url");
   post.setEntity(new InputStreamEntity(new FileInputStream(req_xml), req_xml.length()));
   post.setHeader("Content-type", "text/xml; charset=UTF-8");

        System.out.println("Executing request " + post.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(post);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
            System.out.println(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

1
谢谢,它对我有用。 - Bandana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接