List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:csiro.pidsvc.helper.Http.java
public static String simpleGetRequest(String uri) { HttpClient httpClient = new DefaultHttpClient(); try {/*from ww w. j ava 2s . c o m*/ HttpGet httpGet = new HttpGet(uri); // Get the data. HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); // Return content. return EntityUtils.toString(entity); } catch (Exception e) { _logger.warn("Exception occurred while executing an HTTP request.", e); return null; } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:au.org.ala.bhl.service.WebServiceHelper.java
/** * Performs a GET on the specified URI, and expects that the output is well formed JSON. The output is parsed into a JsonNode for consumption * //from w ww.j a va2 s. c om * @param uri * @return * @throws IOException */ public static JsonNode getJSON(String uri) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); httpget.setHeader("Accept", "application/json"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); @SuppressWarnings("unchecked") List<String> lines = IOUtils.readLines(instream, "utf-8"); String text = StringUtils.join(lines, "\n"); try { JsonNode root = new ObjectMapper().readValue(text, JsonNode.class); return root; } catch (Exception ex) { log("Error parsing results for request: %s\n%s\n", uri, text); ex.printStackTrace(); } } return null; }
From source file:csiro.pidsvc.helper.Http.java
public static String simpleGetRequestStrict(String uri) { HttpClient httpClient = new DefaultHttpClient(); try {// w w w. jav a 2 s . c om HttpGet httpGet = new HttpGet(uri); // Get the data. HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() != 200) return null; HttpEntity entity = response.getEntity(); // Return content. return EntityUtils.toString(entity); } catch (Exception e) { _logger.warn("Exception occurred while executing an HTTP request.", e); return null; } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:org.phpmaven.pear.impl.Helper.java
/** * Returns the text file contents.// w w w . j av a2 s . c o m * @param uri URI of the resource. * @return the files content. * @throws IOException thrown on errors. */ public static String getTextFileContents(String uri) throws IOException { // is it inside the local filesystem? if (uri.startsWith("file://")) { final File channelFile = new File(uri.substring(7)); final String lineSep = System.getProperty("line.separator"); final BufferedReader br = new BufferedReader(new FileReader(channelFile)); String nextLine = ""; final StringBuffer sb = new StringBuffer(); while ((nextLine = br.readLine()) != null) { sb.append(nextLine); sb.append(lineSep); } return sb.toString(); } // try http connection final HttpClient client = new DefaultHttpClient(); final HttpGet httpget = new HttpGet(uri); final HttpResponse response = client.execute(httpget); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / " + response.getStatusLine().getReasonPhrase()); } final HttpEntity entity = response.getEntity(); if (entity == null) { throw new IOException("Empty response."); } return EntityUtils.toString(entity); }
From source file:org.wso2.security.tools.product.manager.NotificationManager.java
private static void notifyStatus(String path, boolean status) throws NotificationManagerException { int i = 0;//ww w . j a va2s . c o m while (i < 10) { try { URI uri = (new URIBuilder()).setHost(automationManagerHost).setPort(automationManagerPort) .setScheme("http").setPath(path).addParameter("containerId", myContainerId) .addParameter("status", String.valueOf(status)).build(); HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet get = new HttpGet(uri); HttpResponse response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { LOGGER.info("Notified successfully"); return; } else { i++; } Thread.sleep(2000); } catch (URISyntaxException | InterruptedException | IOException e) { e.printStackTrace(); LOGGER.error(e.toString()); i++; } } throw new NotificationManagerException("Error occurred while notifying status to Automation Manager"); }
From source file:org.phpmaven.pear.library.impl.Helper.java
/** * Returns the binary file contents.//from w w w. java 2 s . c o m * @param uri URI of the resource. * @return the files content. * @throws IOException thrown on errors. */ public static byte[] getBinaryFileContents(String uri) throws IOException { // is it inside the local filesystem? if (uri.startsWith("file://")) { final File channelFile = new File(uri.substring(7)); final byte[] result = new byte[(int) channelFile.length()]; final FileInputStream fis = new FileInputStream(channelFile); fis.read(result); fis.close(); return result; } // try http connection final HttpClient client = new DefaultHttpClient(); final HttpGet httpget = new HttpGet(uri); final HttpResponse response = client.execute(httpget); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / " + response.getStatusLine().getReasonPhrase()); } final HttpEntity entity = response.getEntity(); if (entity == null) { throw new IOException("Empty response."); } return EntityUtils.toByteArray(entity); }
From source file:es.bsc.demiurge.core.utils.HttpUtils.java
/** * Gets the response of an HTTP request. * /*from w w w. j a v a 2s .co m*/ * @param request The HTTP request from which we want to obtain the response. * @return The response of the HTTP request */ public static String getHttpResponse(HttpRequestBase request) { HttpClient httpclient = HttpClients.createDefault(); String responseContent = ""; try { HttpResponse response = httpclient.execute(request); if (response.getEntity() != null) { responseContent = IOUtils.toString(response.getEntity().getContent()); } } catch (IOException e) { e.printStackTrace(); } return responseContent; }
From source file:it.agileday.utils.HttpRestUtil.java
public static Bitmap httpGetBitmap(String url) { final HttpClient client = new DefaultHttpClient(); final HttpGet getRequest = new HttpGet(url); try {/*from w w w . j a va 2 s . co m*/ HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.w(TAG, "Error " + statusCode + " while retrieving bitmap from " + url); return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = null; try { inputStream = entity.getContent(); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } finally { if (inputStream != null) { inputStream.close(); } entity.consumeContent(); } } } catch (Exception e) { getRequest.abort(); Log.w(TAG, "Error while retrieving bitmap from " + url, e); } return null; }
From source file:eu.skillpro.ams.pscm.connector.amsservice.ui.SendExecutableSkillToServer.java
public static Report push(String id, String snippet, String seeID) throws IOException { String serviceName = "registerResourceExecutableSkill"; StringBuilder parameters = new StringBuilder(); if (id != null && !id.trim().isEmpty()) { parameters.append("?id=" + URLEncoder.encode(id, "UTF-8")); }/* ww w . ja v a2s . c o m*/ if (seeID != null && !seeID.trim().isEmpty()) { parameters.append("&seeID=" + URLEncoder.encode(seeID, "UTF-8")); } if (snippet != null && !snippet.trim().isEmpty()) { parameters.append("&amlDescription=" + URLEncoder.encode(snippet, "UTF-8")); } HttpGet request = new HttpGet(AMSServiceUtility.serviceAddress + serviceName + parameters.toString()); System.out.println(request.getRequestLine() + " ====================================="); request.setHeader("Content-type", "application/json"); HttpClient client = HttpClientBuilder.create().build(); ; HttpResponse response = client.execute(request); System.out.println("Response status: " + response.getStatusLine().getStatusCode()); String resp = EntityUtils.toString(response.getEntity()); Report result = JSONUtility.convertToObject(resp, Report.class); return result; }
From source file:org.basinmc.maven.plugins.minecraft.launcher.VersionIndex.java
/** * Fetches a version index from the Mojang servers. *//*from ww w .jav a 2s. com*/ @Nonnull public static VersionIndex fetch() throws IOException { HttpClient client = HttpClients.createMinimal(); HttpGet request = new HttpGet(URL); HttpResponse response = client.execute(request); StatusLine line = response.getStatusLine(); if (line.getStatusCode() != 200) { throw new IOException( "Unexpected response code: " + line.getStatusCode() + " - " + line.getReasonPhrase()); } try (InputStream inputStream = response.getEntity().getContent()) { return READER.forType(VersionIndex.class).readValue(inputStream); } }