Guest User

testing

a guest
Apr 29th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import org.apache.http.client.ClientProtocolException; //HC version 4.5.5
  2. import org.apache.http.client.methods.CloseableHttpResponse;
  3. import org.apache.http.client.methods.HttpGet;
  4. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  5. import org.apache.http.impl.client.CloseableHttpClient;
  6. import org.apache.http.impl.client.HttpClients;
  7. import org.apache.http.ssl.SSLContexts;
  8.  
  9. import javax.net.ssl.*;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.security.*;
  13. import java.security.cert.CertificateException;
  14. public class TestTwoWay {
  15.  
  16. private static final String KEYSTORE_LOCATION=“<path to p12 file of the client certificate>";
  17.  
  18. private static final String KEYSTORE_PASS=“<client certificate password>";
  19. private static final String SERVER_URL=“<test server host>”;
  20.  
  21. public static final void main(String[] args) {
  22. requestWithHC();
  23. }
  24.  
  25. private SSLContext sslContextBuilderForClientCert(){
  26. KeyStore clientKeyStore;
  27. SSLContext sslContext = null;
  28.  
  29. try(FileInputStream fileInputStream = new FileInputStream(KEYSTORE_LOCATION)) {
  30. clientKeyStore = KeyStore.getInstance("PKCS12");
  31. clientKeyStore.load(fileInputStream, KEYSTORE_PASS.toCharArray());
  32.  
  33. sslContext = SSLContexts.custom().loadKeyMaterial(clientKeyStore, KEYSTORE_PASS.toCharArray()).build();
  34. }catch (IOException ioe){
  35. System.out.println(ioe.getMessage());
  36. }catch (NoSuchAlgorithmException|CertificateException nsce){
  37. System.out.println(nsce.getMessage());
  38. }catch (KeyStoreException kse){
  39. System.out.println(kse.getMessage());
  40. }catch(UnrecoverableKeyException uke){
  41. System.out.println(uke.getMessage());
  42. }catch (KeyManagementException kme){
  43. System.out.println(kme.getMessage());
  44. }
  45. return sslContext;
  46. }
  47. public static void requestWithHC(){
  48. System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
  49. System.setProperty("org.apache.commons.logging.simplelog.showdatetime","true");
  50. System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http","DEBUG");
  51. System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire","ERROR");
  52.  
  53. SSLContext sslContext = new TestTwoWay().sslContextBuilderForClientCert();
  54. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
  55. new String[]{"TLSv1","TLSv1.1","TLSv1.2"},
  56. null,
  57. SSLConnectionSocketFactory.getDefaultHostnameVerifier());
  58. CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
  59. HttpGet httpGet = new HttpGet(SERVER_URL+"/files/test.html”);
  60.  
  61. try{
  62. for(int i=1;i<20;i++) {
  63. CloseableHttpResponse response = httpClient.execute(httpGet);
  64. System.out.println(response.getStatusLine().getStatusCode());
  65. System.out.println(i);
  66. httpGet.releaseConnection();
  67. }
  68.  
  69. }catch(ClientProtocolException cpe){
  70. System.out.println(cpe.getMessage());
  71. }catch(IOException ioe) {
  72. System.out.println(ioe.getMessage());
  73. }
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment