List of usage examples for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler
BasicResponseHandler
From source file:edu.emory.library.utils.EULHttpUtils.java
/** * Utility method to GET the contents of a URL and read it into a string. * @param url url to be read//from w w w .j av a 2 s . com * @param headers HashMap of request headers */ public static String readUrlContents(String url, HashMap<String, String> headers) throws Exception { String response = null; HttpClient client = new DefaultHttpClient(); HttpGet getMethod = new HttpGet(url); // add any request headers specified for (Map.Entry<String, String> header : headers.entrySet()) { getMethod.addHeader(header.getKey(), header.getValue()); } ResponseHandler<String> responseHandler = new BasicResponseHandler(); // returns the response content as string on success response = client.execute(getMethod, responseHandler); // could throw HttpException or IOException // TODO: catch/handle errors (esp. periodic 503 when Spotlight is unavailable) getMethod.releaseConnection(); return response; }
From source file:dbpedia.provenance.AuthorMetadata.java
public Integer WikiUserEditCount(String sUser) { Integer cnt = -1;//www . ja v a 2 s . c o m String url = String.format("http://en.wikipedia.org/w/api.php?action=query&list=users&ususers=" + sUser + "&usprop=editcount&format=xml"); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try { ResponseHandler<String> responseHandler = new BasicResponseHandler(); String response = httpclient.execute(httppost, responseHandler); String pattern = "editcount=\"(.*)\""; Pattern r = Pattern.compile(pattern, Pattern.DOTALL); Matcher m = r.matcher(response); if (m.find()) { cnt = Integer.valueOf(m.group(1)); // System.out.println("number of edits, user " + sUser + ": " + m.group(1) ); } } catch (Exception ex) { System.out.println(ex.getMessage()); } return cnt; }
From source file:com.noelportugal.amazonecho.WitAi.java
public String getJson(String text) { String output = ""; try {/*www . j a v a 2 s .c o m*/ HttpGet httpGet = new HttpGet( "https://api.wit.ai/message?v=20141226&q=" + URLEncoder.encode(text, "UTF-8")); httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token); HttpResponse httpResponse = httpclient.execute(httpGet); StatusLine responseStatus = httpResponse.getStatusLine(); int statusCode = responseStatus.getStatusCode(); if (statusCode == 200) { httpResponse.getEntity(); output = new BasicResponseHandler().handleResponse(httpResponse); } } catch (Exception e) { System.err.println("httpGet Error: " + e.getMessage()); } return output; }
From source file:com.noelportugal.amazonecho.SmartThings.java
public String setApp(String url, String body) { String output = ""; try {// www .j a va 2s . co m HttpPut httpPut = new HttpPut(url); httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpPut.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + bearer); StringEntity xmlEntity = new StringEntity(body); httpPut.setEntity(xmlEntity); HttpResponse httpResponse = httpclient.execute(httpPut); httpResponse.getEntity(); output = new BasicResponseHandler().handleResponse(httpResponse); if (xmlEntity != null) { EntityUtils.consume(xmlEntity); } } catch (Exception e) { System.err.println("setApp Error: " + e.getMessage()); } return output; }
From source file:org.syphr.mythtv.ws.impl.ServiceUtils.java
public static String getVersion(URI serviceBaseUri) throws IOException { URI uri = URI.create(serviceBaseUri.toString() + "/" + VERSION_URI_PATH); HttpClient httpclient = new DefaultHttpClient(); try {//from w ww .j a v a 2 s. c om HttpGet httpget = new HttpGet(uri); LOGGER.debug("Retrieving service version from {}", httpget.getURI()); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); LOGGER.trace("Version response: {}", responseBody); Matcher matcher = VERSION_PATTERN.matcher(responseBody); if (matcher.matches()) { return matcher.group(1); } throw new IOException("Failed to retrieve version information"); } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:br.gov.sc.fatma.resourcemanager.commands.SiteCheckIfUPCommand.java
@Override public Status execute() { Logger.getLogger(SiteCheckIfUPCommand.class.getName()).log(Level.FINE, "-------- " + _site.getPath() + " Connection Testing ------"); Status result;/* w w w . j ava2 s . c o m*/ CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(_site); Logger.getLogger(SiteCheckIfUPCommand.class.getName()).log(Level.INFO, "Executing request " + httpget.getRequestLine()); ResponseHandler<String> responseHandler = new BasicResponseHandler() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { return "200"; } else { return String.valueOf(status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); result = new Status(responseBody.equals("200"), "", Integer.valueOf(responseBody)); } catch (IOException ex) { Logger.getLogger(SiteCheckIfUPCommand.class.getName()).log(Level.WARNING, ex.getMessage(), ex); ex.printStackTrace(); result = new Status(false, ex.getMessage(), -1); } finally { try { httpclient.close(); } catch (IOException ex) { } } return result; }
From source file:it.au.net.iinet.plugins.servlet.MyServletFuncTest.java
@Test public void testSomething() throws IOException { HttpGet httpget = new HttpGet(servletUrl); // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpClient.execute(httpget, responseHandler); assertTrue(null != responseBody && !"".equals(responseBody)); }
From source file:com.fpmislata.banco.business.service.impl.BancoCentralServiceImpl.java
@Override public CredencialesBancarias getURLByCCC(String ccc) throws BusinessException { CloseableHttpClient httpClient = HttpClients.createDefault(); try {/*from ww w . j ava 2 s . c o m*/ HttpGet httpGet = new HttpGet(""); // System.out.println("GET Response Status:: " // + httpResponse.getStatusLine().getStatusCode()); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String response = httpClient.execute(httpGet, responseHandler); return jsonTransformer.fromJSON(response, CredencialesBancarias.class); } catch (IOException ex) { Logger.getLogger(BancoCentralServiceImpl.class.getName()).log(Level.SEVERE, null, ex); // throw new BusinessException("Error por cuestiones ajenas", "BancoCentral"); return this.getURLByCCCLocal(ccc); } finally { try { httpClient.close(); } catch (IOException ex) { Logger.getLogger(BancoCentralServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:br.com.totvs.java3C.fluig.util.FluigAPI.java
public FluigAPI(ItensAmbienteFluig itensAmbienteFluig) { if (itensAmbienteFluig.getModalidade().equals("C")) { // Se o Fluig for "Compartilhado" HttpClient httpClient = new DefaultHttpClient(); try {// ww w . ja v a 2 s. c o m HttpGet httpGet = new HttpGet("http://" + itensAmbienteFluig.getIp() + ":" + itensAmbienteFluig.getPortaWeb() + "/monitor/report"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody; responseBody = httpClient.execute(httpGet, responseHandler); try { jsonObject = new JSONObject(responseBody); goodDataAvailability = (String) jsonObject.getString("goodDataAvailability").trim(); memcachedAvailability = (String) jsonObject.getString("memcachedAvailability").trim(); openOfficeServerAvailability = (String) jsonObject.getString("openOfficeServerAvailability") .trim(); solrServerAvailability = (String) jsonObject.getString("solrServerAvailability").trim(); identityAvailability = (String) jsonObject.getString("identityAvailability").trim(); databaseAvailability = (String) jsonObject.getString("databaseAvailability").trim(); licenseServerAvailability = (String) jsonObject.getString("licenseServerAvailability").trim(); fluigAPIResults = new FluigAPIResults(goodDataAvailability, memcachedAvailability, openOfficeServerAvailability, solrServerAvailability, identityAvailability, databaseAvailability, licenseServerAvailability); } catch (NullPointerException e) { System.out.println("Nao foi possivel obter resposta JSON a partir do Fluig (null)."); System.exit(3); } catch (JSONException j) { System.out.println("JSON invalido: " + responseBody); System.exit(3); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } } }
From source file:org.thiesen.jiffs.jobs.fullpage.PageLoader.java
@Override public void run() { final URI link = _story.getLink(); final HttpGet get = new HttpGet(link); final HttpClient client = new DefaultHttpClient(); try {/* w w w . j av a 2 s . com*/ final String page = client.execute(get, new BasicResponseHandler()); _story.setFullPage(page); _storyDAO.update(_story); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { client.getConnectionManager().shutdown(); } }