Skip to content

Instantly share code, notes, and snippets.

@rupzme
Created July 8, 2016 14:34
Show Gist options
  • Save rupzme/3202384da5a7fb39510d165052948903 to your computer and use it in GitHub Desktop.
Save rupzme/3202384da5a7fb39510d165052948903 to your computer and use it in GitHub Desktop.
package com.company;
/*
Simple TLS1.2 HTTP client for use in java 1.7
Uses Apache libraries:
httpclient-4.5.2.jar
httpcore-4.4.5.jar
commons-codec-1.10.jar
commons-logging-1.2.jar
*/
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
public class Main {
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, IOException {
SSLContext sslContext = sslContext = SSLContexts.custom()
.useTLS()
.build();
SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(
sslContext,
new String[]{"TLSv1.2"},
null,
new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(f)
.build();
HttpGet request = new HttpGet("https://.....");
CloseableHttpResponse response = httpClient.execute(request);
System.out.println("response: "+response);
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String inputLine;
StringBuffer responseJson = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
responseJson.append(inputLine);
}
reader.close();
System.out.println(responseJson.toString());
httpClient.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment