List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:cn.anthony.util.ClientAuthentication.java
public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("httpbin.org", 80), new UsernamePasswordCredentials("user", "passwd")); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {/*from ww w . ja v a 2s .c om*/ HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.http.ClientConnectionRelease.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w ww . j a v a 2 s. c o m HttpGet httpget = new HttpGet("http://www.apache.org/"); // Execute HTTP request System.out.println("executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println("----------------------------------------"); // 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) { InputStream instream = entity.getContent(); try { instream.read(); // do something useful with the response } catch (IOException ex) { // In case of an IOException the connection will be released // back to the connection manager automatically throw ex; } finally { // Closing the input stream will trigger connection release instream.close(); } } } finally { response.close(); } } finally { httpclient.close(); } }
From source file:ATTHackPause.Main.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. j ava2 s. co m*/ HttpGet httpGet = new HttpGet( "https://api-m2x.att.com/v2/devices/ecbcc13b43417c6d9e1dc7618bbb1e6c/streams/"); 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 // and ensure it is fully consumed EntityUtils.consume(entity1); } finally { response1.close(); } HttpPost httpPost = new HttpPost( "https://api-m2x.att.com/v2/devices/ecbcc13b43417c6d9e1dc7618bbb1e6c/streams/Distance/values"); // List <NameValuePair> nvps = new ArrayList <NameValuePair>(); // nvps.add(new BasicNameValuePair("value", "15")); // nvps.add(new BasicNameValuePair("timestamp", "2016-04-09T19:37:42+00:00")); // httpPost.setEntity(new UrlEncodedFormEntity(nvps)); String jsonString = "{\"values\":[{\"value\": \"99\",\"timestamp\": \"2016-04-09T20:43:49+00:00\"}]}"; //System.out.println("\n\njsonString: " + jsonString); // JSONArray jsonArray = new JSONArray(jsonString); // System.out.println("\n\njsonArray: " + jsonArray); StringEntity se = new StringEntity(jsonString); httpPost.setEntity(se); httpPost.setHeader("Content-type", "application/json"); httpPost.setHeader("X-M2X-KEY", "20bfa4a4f5ed88f94d772e8389e9a6d0"); 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.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 w w w. j a v a 2s. c o m*/ 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.zhch.example.commons.http.v4_5.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(new File("d:\\workspace_mars\\resume_import_system\\jssecacerts"), null, new TrustSelfSignedStrategy()) .build();//from w ww . j a v a 2 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 { HttpGet httpget = new HttpGet("https://passport.zhaopin.com/org/login"); 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:ddu.core.httpclient.ClientCustomSSL.java
public final static void main(String[] args) throws Exception { // Trust own CA and all self-signed certs KeyStore trustKeyStore = KeyStore.getInstance("JKS"); // get user password and file input stream char[] password = "123456".toCharArray(); java.io.FileInputStream fis = null; try {/* w w w. java2 s. c om*/ fis = new java.io.FileInputStream("keyStoreName"); trustKeyStore.load(fis, password); } finally { if (fis != null) { fis.close(); } } SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustKeyStore, new TrustSelfSignedStrategy()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { 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.rslakra.testcases.apache.TestApacheHttpClasses.java
/** * /*from w w w . j a v a2 s. 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(); } }
From source file:TestRESTPost12.java
public static void main(String[] p) throws Exception { String strurl = "http://localhost:8080/testnewmaven8/webresources/service/post"; //StringEntity str=new StringEntity("<a>hello post</a>",ContentType.create("application/xml" , Consts.UTF_8)); ////from ww w.j a va 2s . c om StringEntity str = new StringEntity("hello post"); str.setContentType("APPLICATION/xml"); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(strurl); httppost.addHeader("Accept", "application/xml charset=UTF-8"); //httppost.addHeader("content_type", "application/xml, multipart/related"); httppost.setEntity(str); CloseableHttpResponse response = httpclient.execute(httppost); // try //{ int statuscode = response.getStatusLine().getStatusCode(); if (statuscode != 200) { System.out.println("http error occured=" + statuscode); } BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while (br.readLine() != null) { System.out.println(br.readLine()); } // } /*catch(Exception e) { System.out.println("exception :"+e); }*/ //httpclient.close(); }
From source file:MyTest.DownloadFileTest.java
public static void main(String args[]) { CloseableHttpClient httpclient = HttpClients.createDefault(); String url = "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7"; System.out.println(url.charAt(50)); HttpGet httpget = new HttpGet(url); HttpEntity entity = null;/*from ww w . j a v a 2 s . co m*/ try { HttpResponse response = httpclient.execute(httpget); entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); String filename = "testdata/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow"; BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename))); int readedByte; while ((readedByte = bis.read()) != -1) { bos.write(readedByte); } bis.close(); bos.close(); } httpclient.close(); } catch (IOException ex) { Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex); } }
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 {//from w w w. j ava 2 s . c o m 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(); } }