2016-11-25 264 views
2

我想擊中客戶端認證的URL必須生成與關鍵:SOAP請求與客戶端認證連接HTTP客戶端超時異常

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; 

}

回答

2

試一試這段代碼,希望它能幫到你。

 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(); 
    } 
+0

感謝它對我有用 – Bandana