Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.client.ClientProtocolException; //HC version 4.5.5
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.ssl.SSLContexts;
- import javax.net.ssl.*;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.security.*;
- import java.security.cert.CertificateException;
- public class TestTwoWay {
- private static final String KEYSTORE_LOCATION=“<path to p12 file of the client certificate>";
- private static final String KEYSTORE_PASS=“<client certificate password>";
- private static final String SERVER_URL=“<test server host>”;
- public static final void main(String[] args) {
- requestWithHC();
- }
- private SSLContext sslContextBuilderForClientCert(){
- KeyStore clientKeyStore;
- SSLContext sslContext = null;
- try(FileInputStream fileInputStream = new FileInputStream(KEYSTORE_LOCATION)) {
- clientKeyStore = KeyStore.getInstance("PKCS12");
- clientKeyStore.load(fileInputStream, KEYSTORE_PASS.toCharArray());
- sslContext = SSLContexts.custom().loadKeyMaterial(clientKeyStore, KEYSTORE_PASS.toCharArray()).build();
- }catch (IOException ioe){
- System.out.println(ioe.getMessage());
- }catch (NoSuchAlgorithmException|CertificateException nsce){
- System.out.println(nsce.getMessage());
- }catch (KeyStoreException kse){
- System.out.println(kse.getMessage());
- }catch(UnrecoverableKeyException uke){
- System.out.println(uke.getMessage());
- }catch (KeyManagementException kme){
- System.out.println(kme.getMessage());
- }
- return sslContext;
- }
- public static void requestWithHC(){
- System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
- System.setProperty("org.apache.commons.logging.simplelog.showdatetime","true");
- System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http","DEBUG");
- System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire","ERROR");
- SSLContext sslContext = new TestTwoWay().sslContextBuilderForClientCert();
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
- new String[]{"TLSv1","TLSv1.1","TLSv1.2"},
- null,
- SSLConnectionSocketFactory.getDefaultHostnameVerifier());
- CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
- HttpGet httpGet = new HttpGet(SERVER_URL+"/files/test.html”);
- try{
- for(int i=1;i<20;i++) {
- CloseableHttpResponse response = httpClient.execute(httpGet);
- System.out.println(response.getStatusLine().getStatusCode());
- System.out.println(i);
- httpGet.releaseConnection();
- }
- }catch(ClientProtocolException cpe){
- System.out.println(cpe.getMessage());
- }catch(IOException ioe) {
- System.out.println(ioe.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment