package com.kt4study.apache.http;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
/**
* @author kt4study.blogspot.co.uk
*
*/
public class DeprecatedHTTPClient {
public static void removeDepricatedHTTPClientDemo()
throws AuthenticationException, ClientProtocolException, IOException, NoSuchAlgorithmException {
/*//Depricated DefaultHttpClient
HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient();
HttpGet httpGetRequest = new HttpGet("http://kt4study.blogspot.co.uk/");
HttpResponse response = client.execute(request);
*/
HttpClient httpClient = HttpClientBuilder.create().build();
//Change your URL
HttpGet httpGetRequest = new HttpGet("http://kt4study.blogspot.co.uk");
UsernamePasswordCredentials credential = new UsernamePasswordCredentials("username", "password");
//HTTP/HTTPS Basis Authentication
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(credential, httpGetRequest, null);
httpGetRequest.addHeader(header);
//Set SSL for Authentication
final SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(SSLContext.getDefault(),NoopHostnameVerifier.INSTANCE);
final Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http", new PlainConnectionSocketFactory())
.register("https", sslConnectionSocketFactory).build();
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(100);
httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(connectionManager).build();
//Execute httpClient after setting SSL
HttpResponse httpResponse = httpClient.execute(httpGetRequest);
HttpEntity responseEntity = httpResponse.getEntity();
//Http Response into String
String response = EntityUtils.toString(responseEntity);
System.out.println("HTTP Response Message :: " + response);
}
public static void main(String[] args)
throws AuthenticationException, ClientProtocolException, NoSuchAlgorithmException, IOException {
DeprecatedHTTPClient.removeDepricatedHTTPClientDemo();
}
}
|
No comments:
Post a Comment