List of usage examples for org.apache.http.client ResponseHandler ResponseHandler
ResponseHandler
From source file:org.openmrs.contrib.discohub.HttpUtils.java
public static Map<String, Object> getData(String url, Header[] headers) throws IOException { final Map<String, Object> responseMap = new LinkedHashMap<>(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); httpget.setHeaders(headers);//from ww w . j a v a2 s .c om // 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(); responseMap.put("headers", response.getAllHeaders()); //System.out.println("Ratelimit attempts left: " + response.getHeaders("X-RateLimit-Remaining")[0]); //System.out.println("Etag = " + response.getHeaders("ETag")[0]); return entity != null ? EntityUtils.toString(entity) : null; } else if (status == 304) { //System.out.println("GOT 304!!!"); return null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); responseMap.put("content", responseBody); return responseMap; }
From source file:com.javaquery.aws.elasticsearch.GetExample.java
/** * Perform get request./*from w w w . j a v a 2 s .c om*/ * @param httpGet */ public static void httpGetRequest(HttpGet httpGet) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpGet, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.mobile.mpos.util.HttpClientHelper.java
/** * get request//from w ww . ja va2s . c o m * @param uri * @return */ public static String get(String uri) { CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(uri); log.info("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); log.info(" response " + responseBody); return responseBody; } catch (ClientProtocolException e) { log.error("ClientProtocolException " + e.getMessage()); } catch (IOException e) { log.error("IOException " + e.getMessage()); } finally { close(httpClient); } return null; }
From source file:com.javaquery.aws.elasticsearch.DeleteExample.java
/** * Perform delete request./* ww w . java2 s . c o m*/ * @param httpDelete */ public static void httpDeleteRequest(HttpDelete httpDelete) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpDelete, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.uber.stream.kafka.mirrormaker.common.utils.HttpClientUtils.java
public static ResponseHandler<String> createResponseBodyExtractor() { return new ResponseHandler<String>() { @Override//ww w . ja va2 s. c o m public String handleResponse(final HttpResponse response) throws 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 while getting /topics : " + status); } } }; }
From source file:com.mycompany.rakornas.getDataURL.java
String getData(String url) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet(url); System.out.println("Executing request " + httpget.getRequestLine()); ResponseHandler<String> responseHandler; responseHandler = new ResponseHandler<String>() { @Override/*from w w w. ja v a2 s .c om*/ 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 responseURL = httpclient.execute(httpget, responseHandler); return responseURL; } }
From source file:org.winardiaris.uangku.getDataURL.java
String getData(String url) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w . j ava 2 s . co m HttpGet httpget = new HttpGet(url); System.out.println("Executing request " + httpget.getRequestLine()); ResponseHandler<String> responseHandler; 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 responseURL = httpclient.execute(httpget, responseHandler); return responseURL; } finally { httpclient.close(); } }
From source file:com.javaquery.aws.elasticsearch.AddUpdateExample.java
/** * Perform post request.//w ww . j a va 2 s . c o m * @param httpPost */ public static void httpPostRequest(HttpPost httpPost) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPost, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.johnson.grab.browser.HttpClientUtil.java
/** * Get content by url as string//from ww w.j a va 2 s. c o m * @param url * original url * @return * page content * @throws java.io.IOException */ public static String getContent(String url) throws CrawlException, IOException { // construct request HttpGet request = new HttpGet(url); request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT) .setSocketTimeout(SOCKECT_TIMEOUT).build()); // construct response handler ResponseHandler<String> handler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); // status if (status.getStatusCode() != HttpStatus.SC_OK) { throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()); } // get encoding in header String encoding = getPageEncoding(response); boolean encodingFounded = true; if (StringUtil.isEmpty(encoding)) { encodingFounded = false; encoding = UniversalConstants.Encoding.ISO_8859_1; } // get content and find real encoding HttpEntity entity = response.getEntity(); if (entity == null) { return null; } // get content byte[] contentBytes = EntityUtils.toByteArray(entity); if (contentBytes == null) { return null; } // found encoding if (encodingFounded) { return new String(contentBytes, encoding); } // attempt to discover encoding String rawContent = new String(contentBytes, UniversalConstants.Encoding.DEFAULT); Matcher matcher = PATTERN_HTML_CHARSET.matcher(rawContent); if (matcher.find()) { String realEncoding = matcher.group(1); if (!encoding.equalsIgnoreCase(realEncoding)) { // bad luck :( return new String(rawContent.getBytes(encoding), realEncoding); } } // not found right encoding :) return rawContent; } }; // execute CloseableHttpClient client = HttpClientHolder.getClient(); return client.execute(request, handler); }
From source file:com.kingmed.dp.ndp.NDPConnectionManager.java
@Override public void connect() throws Exception { String signinUrl = ndpServe.getUrlSignin(); CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from ww w .j a v a2 s . c o m HttpGet httpget = new HttpGet(signinUrl); String responseBody = httpclient.execute(httpget, new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse hr) throws ClientProtocolException, IOException { int status = hr.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = hr.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }); } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } }