List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:com.http.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {// w w w.j av a 2 s.com HttpGet httpget = new HttpGet("https://localhost/protected"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:baseFrame.netty.atest.QuickTest.java
public static void main(String[] args) throws Exception { for (int i = 0; i < 100000; i++) { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from ww w .j av a2 s. co m*/ HttpGet httpGet = new HttpGet("http://127.0.0.1:9990"); CloseableHttpResponse response1 = httpclient.execute(httpGet); long startTime = System.nanoTime(); System.out.println(response1.getStatusLine()); HttpEntity entity = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // System.out.println(entity.getContentType()); long endTime = System.nanoTime(); System.out.println("-----" + (endTime - startTime)); } finally { httpclient.close(); } } /* CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet("http://127.0.0.1:8080"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed // System.out.println(entity.getContentType()); //?? System.out.println(entity.getContentEncoding()); // System.out.println(entity.getContentLength()); //? System.out.println(EntityUtils.toString(entity)); //? //System.out.println(EntityUtils.toByteArray(entity).length); //? //entity.getContent(); EntityUtils.consume(entity); } finally { response1.close(); } HttpPost httpPost = new HttpPost("http://targethost/login"); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("username", "vip")); nvps.add(new BasicNameValuePair("password", "secret")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); }*/ }
From source file:eu.transcriptorium.trpclient.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("dbis-faxe.uibk.ac.at", 443), new UsernamePasswordCredentials(user, password)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try //Set-Cookie: JSESSIONID=ABAD1D;path=/ {//from w ww .j ava2 s . c o m HttpGet httpget = new HttpGet("https://dbis-faxe.uibk.ac.at/TrpServerTesting/rest/docs/62/fulldoc.xml"); //httpclient. httpget.setHeader("Cookie", "JSESSIONID=5FB6AC0CD0F4FAC80CE6716DD789F5E8"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.lxf.spider.client.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("my.keystore")); try {//from ww w .j a va 2 s. c om trustStore.load(instream, "nopassword".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpGet httpget = new HttpGet("https://localhost/"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.sim.demo.httpclient.httpget.SinaDemo.java
public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); // HttpGet//from w w w .ja v a 2s. c om HttpGet httpGet = new HttpGet("http://www.sina.com"); System.out.println("executing request " + httpGet.getURI()); // get HttpResponse response = httpClient.execute(httpGet); // ?? HttpEntity entity = response.getEntity(); // ??? System.out.println(response.getStatusLine()); if (entity != null) { // ?? System.out.println("Response content lenght:" + entity.getContentLength()); String content = EntityUtils.toString(entity, "UTF-8").trim(); // HttpClient?? String? System.out.println("Response content:" + content); } // ? httpClient.close(); }
From source file:com.lxf.spider.client.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. j a v a 2s . c o m*/ HttpHost target = new HttpHost("localhost", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.orange.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w ww .j ava 2 s . co m*/ HttpGet httpget = new HttpGet("http://httpbin.org/get"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { mRead(entity); } } finally { response.close(); } } finally { httpclient.close(); } }
From source file:test.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("D:\\keystore.jks")); try {// w w w . java 2 s.c om trustStore.load(instream, "password".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpGet httpget = new HttpGet("https://retail.onlinesbi.com/personal/css/style.css"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.luqili.http.ssl.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { String sslKeyStorePath = "/mnt/data2/clientkey/370900.pfx"; String sslKeyStorePassword = "370900"; String sslKeyStoreType = "PKCS12"; // JKS PKCS12 String sslTrustStore = "/mnt/data2/clientkey/PengeSoftOARoot.cer"; String sslTrustStorePassword = ""; String url = "https://123.57.205.217:8082/Service/UpReportInfoServiceSvr.assx/UpReportInfo"; String content = "Token=&CityCodes=370900&CountyCodes=&DealDate=2016-06-16T00:00:00&Reportor=?&NewBusinessArea=1000.88&NewBusinessAmount=9898&NewHouseArea=5000&NewHouseAmount=8000&NewHouseCount=20&OldReportor=?&OldBusinessArea=1234.56&OldBusinessAmount=80000&OldHouseArea=12580&OldHouseAmount=99999&OldHouseCount=100"; // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(new File(sslKeyStorePath), sslKeyStorePassword.toCharArray(), new TrustSelfSignedStrategy()) .loadKeyMaterial(new File(sslKeyStorePath), sslKeyStorePassword.toCharArray(), sslKeyStorePassword.toCharArray()) .build();//from w w w . j a v a2 s.co m // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpPost httppost = new HttpPost(url); System.out.println("Executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8").trim(); if (!"True".toUpperCase().equals(result.toUpperCase())) { System.out.println(result); } } System.out.println(response.getStatusLine()); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.orange.ClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from ww w .j av a 2 s . c om*/ HttpHost target = new HttpHost("httpbin.org", 443, "https"); HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("/"); request.setConfig(config); System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } }