List of usage examples for org.apache.http.impl.client HttpClients createDefault
public static CloseableHttpClient createDefault()
From source file:com.li.tools.httpclient.util.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. ja v a 2s .c o m*/ HttpGet httpget = new HttpGet("http://httpbin.org/"); System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:nl.knaw.dans.cmd2rdf.conversion.util.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/* w ww .java2s . c o m*/ HttpGet httpget = new HttpGet("http://localhost/"); //System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); //System.out.println("----------------------------------------"); //System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:demo.example.ClientChunkEncodedPost.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("File path not given"); System.exit(1);//from w w w . j a va2s . c o m } CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost("http://httpbin.org/post"); File file = new File(args[0]); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM); reqEntity.setChunked(true); // It may be more appropriate to use FileEntity class in this particular // instance but we are using a more generic InputStreamEntity to demonstrate // the capability to stream out data from any arbitrary source // // FileEntity entity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity); System.out.println("Executing request: " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); 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:interoperabilite.webservice.client.ClientCustomContext.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w ww . ja v a 2s .c o m*/ // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpClientContext localContext = HttpClientContext.create(); // Bind custom cookie store to the local context localContext.setCookieStore(cookieStore); HttpGet httpget = new HttpGet("http://httpbin.org/cookies"); System.out.println("Executing request " + httpget.getRequestLine()); // Pass local context as a parameter CloseableHttpResponse response = httpclient.execute(httpget, localContext); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:myexamples.QuickStart.java
public static void main(String[] args) throws Exception { Gson gson = new Gson(); CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*w w w .ja v a 2 s. c o m*/ HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9"); 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 if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < 2048) { System.out.println(EntityUtils.toString(entity1)); } else { System.out.println("entity length=" + len); } } EntityUtils.consume(entity1); } 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:com.http.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w w w .j a va 2 s. c o m HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); System.out.println(response.getStatusLine()); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); entity = new BufferedHttpEntity(entity); System.out.println("---1-----:" + EntityUtils.toString(entity)); System.out.println("---2-----:" + EntityUtils.toString(entity)); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { httpclient.close(); } }
From source file:httpclientdemo.ClientChunkEncodedPost.java
public static void main(String[] args) throws Exception { // if (args.length != 1) { // System.out.println("File path not given"); // System.exit(1); // }/*from w ww .j a va2 s . c o m*/ CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost("http://httpbin.org/post"); File file = new File("/Users/jack/Downloads/listdata (1).xlsx"); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM); reqEntity.setChunked(true); // It may be more appropriate to use FileEntity class in this particular // instance but we are using a more generic InputStreamEntity to demonstrate // the capability to stream out data from any arbitrary source // // FileEntity entity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity); System.out.println("Executing request: " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); 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.jivesoftware.os.amza.service.AmzaSetStress.java
public static void main(String[] args) throws IOException { args = new String[] { "soa-integ-data12.phx1.jivehosted.com", "1185", "1", "10000" }; final String hostName = args[0]; final int port = Integer.parseInt(args[1]); final int firstDocId = Integer.parseInt(args[2]); final int count = Integer.parseInt(args[3]); final int batchSize = 100; String partitionName = "lorem"; for (int i = 0; i < 8; i++) { final String rname = partitionName + i; final org.apache.http.client.HttpClient httpClient = HttpClients.createDefault(); Thread t = new Thread() { @Override//from w ww . j a va 2 s.co m public void run() { try { feed(httpClient, hostName, port, rname, 0, count, batchSize); } catch (Exception x) { x.printStackTrace(); } } }; t.start(); } }
From source file:com.oocl.euc.ita.test.QuickStart.java
public static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w .j av a2 s.c o m*/ // HttpPost? HttpPost httpPost = new HttpPost("http://localhost:8080/qb-webapp/login"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "Neolarry")); nvps.add(new BasicNameValuePair("password", "123456789")); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); 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(); } HttpGet httpGet = new HttpGet("http://localhost:8080/qb-webapp/api/trainer"); System.out.println(httpGet.getURI()); 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("get: "+response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed String responseString = ""; if (entity1 != null) { // // System.out.println("Response content length: " + entity1.getContentLength()); System.out.println(responseString = EntityUtils.toString(entity1)); // ? } //JsonToObject // JSONArray s = JSONArray.parseArray(responseString); // for (int i = 0; i < s.size(); i++) { // System.out.println(s.get(i)); // } EntityUtils.consume(entity1); } finally { response1.close(); } } finally { httpclient.close(); } }
From source file:demo.example.ClientMultipartFormPost.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("File path not given"); System.exit(1);/*from ww w.j a v a 2 s . com*/ } CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost( "http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample"); FileBody bin = new FileBody(new File(args[0])); StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment) .build(); 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(); } }