List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:com.cland.accessstats.util.QuickStart.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w ww . j a v a 2s. c o m*/ String origins = "-33.869952,18.537687"; String destinations = "-33.96979309231926,18.53386491408955|-33.90630759128996,18.40310809285091|-33.97761,18.46555"; String avoid = GMatrix.AVOID_HIGHWAYS + "|" + GMatrix.AVOID_TOLLS; String units = GMatrix.UNITS_DEFAULT; String language = ""; boolean sensor = true; String url = GMatrix.getUrl(origins, destinations, GMatrix.JSON, GMatrix.MOD_DRIVING, avoid, units, language, sensor); System.out.println("Connection to... " + url); //GET EXAMPLE HttpGet httpGet = new HttpGet(url); 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 entity1 = response1.getEntity(); // do something useful with the response body String result = EntityUtils.toString(entity1); // and ensure it is fully consumed EntityUtils.consume(entity1); //Process the result json data in the the into a collection DataEntry objects int num = 3; //we requested for 3 destinations List<DataEntry> dataentries = new ArrayList<DataEntry>(); for (int i = 0; i < num; i++) { String distance = GMatrix.getDistanceText(result, 0, i); String duration = GMatrix.getDurationText(result, 0, i); DataEntry d = new DataEntry(); d.setName("DESTINATION " + (i + 1)); d.setDistance(distance); d.setTravelTime(duration); dataentries.add(d); // System.out.println(">>> DESTINATION " + i); // System.out.println(">>>Distance text: " + distance); // System.out.println(">>>Duration : " + duration); } //end for loop //Now we can return the collection as JSON at this ONLY has google data only //Lets see wat we have so far for (DataEntry e : dataentries) { System.out.println(">>> " + e.getName() + ": Distance=" + e.getDistance() + ", Duration=" + e.getTravelTime()); } } finally { response1.close(); } // POST EXAMPLE // 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:com.wxpay.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("E:/apiclient_cert1.p12")); try {/*w ww . j a v a 2s . c o m*/ keyStore.load(instream, "1269885501".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1269885501".toCharArray()).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://api.mch.weixin.qq.com/secapi/pay/refund"); 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()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:org.gzk.image.junit.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:/apiclient_cert.p12")); try {/*from w ww . ja v a 2 s . c o m*/ keyStore.load(instream, "1374938902".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1374938902".toCharArray()).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://api.mch.weixin.qq.com/secapi/pay/refund"); 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()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.magicbeans.banjiuwan.util.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:/10016225.p12")); try {/* w w w. j a va2s .c o m*/ keyStore.load(instream, "10016225".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "10016225".toCharArray()).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://api.mch.weixin.qq.com/secapi/pay/refund"); 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()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.lexmark.saperion.util.ClientMultipartFormPost.java
public static void main(String[] args) throws Exception { String userName = "amolugu"; String password = "ecm"; String authString = userName + ":" + password; byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from ww w .j a va 2 s . c o m HttpPost httpPost = new HttpPost("https://ecm-service.psft.co/ecms/documents"); //http httpPost.addHeader("Authorization", "Basic " + authStringEnc); httpPost.addHeader("Accept", "application/json"); httpPost.addHeader("saTenant", "india"); httpPost.addHeader("saLicense", "1"); httpPost.addHeader("Content-Type", "application/octet-stream"); FileBody bin = new FileBody(new File("C:\\Users\\Aditya.Molugu\\workspace\\RestClient\\Binaries.txt")); StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment) .build(); //HttpBo httpPost.setEntity(reqEntity); System.out.println("executing request " + httpPost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:org.ege.httpclient.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials("alice", "alice")); Lookup<AuthSchemeProvider> basicAuthSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.BASIC, new BasicSchemeFactory()).build(); Lookup<AuthSchemeProvider> spnegoAuthSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .setDefaultAuthSchemeRegistry(spnegoAuthSchemeRegistry).build(); //Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).build(); //Lookup<AuthSchemeProvider> authRegistry = <...> //HttpClientContext context = HttpClientContext.create(); //context.setCredentialsProvider(credsProvider); //context.setAuthSchemeRegistry(authRegistry); try {/* ww w .j a va 2s . co m*/ HttpGet httpget = new HttpGet("http://lcom501d/hooks/hdr.cgi"); 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:interoperabilite.webservice.client.ClientCustomPublicSuffixList.java
public final static void main(String[] args) throws Exception { // Use PublicSuffixMatcherLoader to load public suffix list from a file, // resource or from an arbitrary URL PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader .load(new URL("https://publicsuffix.org/list/effective_tld_names.dat")); // Please use the publicsuffix.org URL to download the list no more than once per day !!! // Please consider making a local copy !!! DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); RFC6265CookieSpecProvider cookieSpecProvider = new RFC6265CookieSpecProvider(publicSuffixMatcher); Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create() .register(CookieSpecs.DEFAULT, cookieSpecProvider) .register(CookieSpecs.STANDARD, cookieSpecProvider) .register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider).build(); CloseableHttpClient httpclient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier) .setDefaultCookieSpecRegistry(cookieSpecRegistry).build(); try {/*www . j a va2 s . co m*/ HttpGet httpget = new HttpGet("https://httpbin.org/"); 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()); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveBasicAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("localhost", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*from ww w . j a v a 2 s . c o m*/ httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("username", "password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); 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 { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:org.slieer.http.auth.ClientPreemptiveBasicAuthentication.java
public static void main(String[] args) throws Exception { HttpHost targetHost = new HttpHost("localhost", 8080, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*from www . j a va 2 s.c o m*/ httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("newuser", "tomcat")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/simpleweb/protected"); System.out.println("executing request: " + httpget.getRequestLine()); System.out.println("to target: " + targetHost); for (int i = 0; i < 3; i++) { HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); 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 { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.rslakra.testcases.apache.TestApacheHttpClasses.java
/** * /*from ww w . j a v a 2s. c om*/ * @param args * @throws Exception */ public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); final String urlString = "https://qawest.meetx.org/"; CloseableHttpResponse httpResponse = null; try { HttpGet httpGet = new HttpGet(urlString); httpResponse = 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(httpResponse.getStatusLine()); Header[] headers = httpGet.getAllHeaders(); for (Header header : headers) { System.out.println(header.getName() + "=" + header.getValue()); } HttpEntity httpEntity = httpResponse.getEntity(); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(httpEntity); } finally { if (httpResponse != null) { httpResponse.close(); } } // HttpPost httpPost = new HttpPost(urlString); // 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(); } }