List of usage examples for org.apache.http.client ResponseHandler ResponseHandler
ResponseHandler
From source file:com.javaquery.apache.httpclient.HttpDeleteExample.java
public static void main(String[] args) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Prepare delete request */ HttpDelete httpDelete = new HttpDelete("http://www.example.com/api/customer"); /* Add headers to get request */ httpDelete.addHeader("Authorization", "value"); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override/* w w w . j av a 2s .c o m*/ public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { /* Get status code */ int httpResponseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Response code: " + httpResponseCode); if (httpResponseCode >= 200 && httpResponseCode < 300) { /* Convert response to String */ HttpEntity entity = httpResponse.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { return null; /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */ } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpDelete, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.javaquery.apache.httpclient.HttpGetExample.java
public static void main(String[] args) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Prepare get request */ HttpGet httpGet = new HttpGet("http://www.example.com/api/customer"); /* Add headers to get request */ httpGet.addHeader("Authorization", "value"); httpGet.addHeader("Content-Type", "application/json"); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override// w w w. j a v a 2 s.c o m public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { /* Get status code */ int httpResponseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Response code: " + httpResponseCode); if (httpResponseCode >= 200 && httpResponseCode < 300) { /* Convert response to String */ HttpEntity entity = httpResponse.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { return null; /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */ } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpGet, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:downloadwolkflow.httpTest.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*w ww. j a va 2 s . c o m*/ HttpGet httpget = new HttpGet( "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7"); //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:com.javaquery.apache.httpclient.HttpPutExample.java
public static void main(String[] args) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Prepare put request */ HttpPut httpPut = new HttpPut("http://www.example.com/api/customer"); /* Add headers to get request */ httpPut.addHeader("Authorization", "value"); /* Prepare StringEntity from JSON */ StringEntity jsonData = new StringEntity("{\"id\":\"123\", \"name\":\"Vicky Thakor\"}", "UTF-8"); /* Body of request */ httpPut.setEntity(jsonData);/*from w ww . j av a 2 s. c o m*/ /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { /* Get status code */ int httpResponseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Response code: " + httpResponseCode); if (httpResponseCode >= 200 && httpResponseCode < 300) { /* Convert response to String */ HttpEntity entity = httpResponse.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { return null; /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */ } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPut, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.javaquery.apache.httpclient.HttpPostExample.java
public static void main(String[] args) throws UnsupportedEncodingException { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Prepare POST request */ HttpPost httpPost = new HttpPost("http://www.example.com/api/customer"); /* Add headers to POST request */ httpPost.addHeader("Authorization", "value"); httpPost.addHeader("Content-Type", "application/json"); /* Prepare StringEntity from JSON */ StringEntity jsonData = new StringEntity("{\"id\":\"123\", \"name\":\"Vicky Thakor\"}", "UTF-8"); /* Body of request */ httpPost.setEntity(jsonData);/*from ww w . ja v a 2 s . c o m*/ /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { /* Get status code */ int httpResponseCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Response code: " + httpResponseCode); if (httpResponseCode >= 200 && httpResponseCode < 300) { /* Convert response to String */ HttpEntity entity = httpResponse.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { return null; /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */ } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPost, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.ny.apps.executor.httpclient.HttpRequestSender.java
public static void main(String[] args) throws ClientProtocolException, IOException { /*//from ww w .java 2 s. c o m CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", 80), new UsernamePasswordCredentials("root", "123")); CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); */ CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.baidu.com/"); httpGet.setHeader("Authorization", ""); logger.info("executing request " + httpGet.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // TODO ? int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity); } else { throw new ClientProtocolException("Unexpected response status: " + statusCode); } } }; try { String responseBody = client.execute(httpGet, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { client.close(); } }
From source file:com.lxf.spider.client.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w .j a va 2s .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: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. j av a 2 s. co 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 {//from w w w .ja va 2s . 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:interoperabilite.webservice.fluent.FluentResponseHandling.java
public static void main(String[] args) throws Exception { Document result = Request.Get("http://somehost/content").execute() .handleResponse(new ResponseHandler<Document>() { @Override// w w w. j av a 2s .c om public Document handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); ContentType contentType = ContentType.getOrDefault(entity); if (!contentType.equals(ContentType.APPLICATION_XML)) { throw new ClientProtocolException("Unexpected content type:" + contentType); } Charset charset = contentType.getCharset(); if (charset == null) { charset = Consts.ISO_8859_1; } return docBuilder.parse(entity.getContent(), charset.name()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }); // Do something useful with the result System.out.println(result); }