List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:com.evolveum.midpoint.report.impl.ReportNodeUtils.java
public static InputStream executeOperation(String host, String fileName, String intraClusterHttpUrlPattern, String operation) throws CommunicationException, SecurityViolationException, ObjectNotFoundException, ConfigurationException, IOException { fileName = fileName.replaceAll("\\s", SPACE); InputStream inputStream = null; InputStream entityContent = null; LOGGER.trace("About to initiate connection with {}", host); try {// w ww . j a v a2s. c o m if (StringUtils.isNotEmpty(intraClusterHttpUrlPattern)) { LOGGER.trace("The cluster uri pattern: {} ", intraClusterHttpUrlPattern); URI requestUri = buildURI(intraClusterHttpUrlPattern, host, fileName); fileName = URLDecoder.decode(fileName, ReportTypeUtil.URLENCODING); LOGGER.debug("Sending request to the following uri: {} ", requestUri); HttpRequestBase httpRequest = buildHttpRequest(operation); httpRequest.setURI(requestUri); httpRequest.setHeader("User-Agent", ReportTypeUtil.HEADER_USERAGENT); HttpClient client = HttpClientBuilder.create().build(); try (CloseableHttpResponse response = (CloseableHttpResponse) client.execute(httpRequest)) { HttpEntity entity = response.getEntity(); Integer statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { LOGGER.debug("Response OK, the file successfully returned by the cluster peer. "); if (entity != null) { entityContent = entity.getContent(); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = entityContent.read(buffer)) > -1) { arrayOutputStream.write(buffer, 0, len); } arrayOutputStream.flush(); inputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray()); } } else if (statusCode == HttpStatus.SC_NO_CONTENT) { if (HttpDelete.METHOD_NAME.equals(operation)) { LOGGER.info("Deletion of the file {} was successful.", fileName); } } else if (statusCode == HttpStatus.SC_FORBIDDEN) { LOGGER.error("The access to the report with the name {} is forbidden.", fileName); String error = "The access to the report " + fileName + " is forbidden."; throw new SecurityViolationException(error); } else if (statusCode == HttpStatus.SC_NOT_FOUND) { String error = "The report file " + fileName + " was not found on the originating nodes filesystem."; throw new ObjectNotFoundException(error); } } catch (ClientProtocolException e) { String error = "An exception with the communication protocol has occurred during a query to the cluster peer. " + e.getLocalizedMessage(); throw new CommunicationException(error); } } else { LOGGER.error( "Cluster pattern parameters is empty, please refer to the documentation and set up the parameter value accordingly"); throw new ConfigurationException( "Cluster pattern parameters is empty, please refer to the documentation and set up the parameter value accordingly"); } } catch (URISyntaxException e1) { throw new CommunicationException("Invalid uri syntax: " + e1.getLocalizedMessage()); } catch (UnsupportedEncodingException e) { LOGGER.error("Unhandled exception when listing nodes"); LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing nodes", e); } finally { IOUtils.closeQuietly(entityContent); } return inputStream; }
From source file:com.iti.request.NearbyService.java
public static String httpGet(String url) throws IOException { //connect to url HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response;/* www . jav a 2 s. c o m*/ response = client.execute(get); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuffer result = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { result.append(line); } return result.toString(); }
From source file:uk.ac.ebi.atlas.utils.HttpRequest.java
public static InputStream httpPost(org.apache.http.client.HttpClient httpClient, String url, List<? extends NameValuePair> params) throws IOException { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return response.getEntity().getContent(); }/*from w ww . j av a 2s .co m*/ throw new IOException( "Server returned invalid response: [status_code = " + statusCode + "; url = " + url + "]"); }
From source file:core.RESTCalls.RESTPost.java
public static String httpPost(String urlStr) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlStr); HttpResponse response = client.execute(post); BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder total = new StringBuilder(); String line = null;/*from w w w . j a v a 2 s. c o m*/ while ((line = r.readLine()) != null) total.append(line + "\n"); return total.toString(); }
From source file:com.rogiel.httpchannel.util.HttpClientUtils.java
public static Future<HttpResponse> executeAsyncHttpResponse(final HttpClient client, final HttpUriRequest request) throws IOException { return threadPool.submit(new Callable<HttpResponse>() { @Override/*from w ww.j a v a2s . com*/ public HttpResponse call() throws Exception { return client.execute(request); } }); }
From source file:jfabrix101.lib.helper.NetworkHelper.java
/** * Last part of execution an http request. * Execute a request and return the body. * @param httpClient/*from w w w . ja va 2s.co m*/ * @param httpRequest * @return * @throws Exception */ private static String execHttpRequest(HttpClient httpClient, HttpRequestBase httpRequest) throws Exception { HttpResponse response = httpClient.execute(httpRequest); int returnCode = response.getStatusLine().getStatusCode(); String htmlBody = EntityUtils.toString(response.getEntity()); if (returnCode != HttpStatus.SC_OK) { mLogger.error("- Network error reading URL: " + httpRequest.getURI().toString()); mLogger.error("- Network error: HttpStatusCode : " + response.getStatusLine().getStatusCode()); return null; } return htmlBody; }
From source file:com.foundationdb.http.HttpThreadedLoginIT.java
private static int openRestURL(String userInfo, int port, String path) throws Exception { HttpClient client = new DefaultHttpClient(); URI uri = new URI("http", userInfo, "localhost", port, path, null, null); HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); int code = response.getStatusLine().getStatusCode(); EntityUtils.consume(response.getEntity()); client.getConnectionManager().shutdown(); return code;/* www.j ava2s.c om*/ }
From source file:org.apache.heron.integration_topology_test.core.HttpUtils.java
public static int httpJsonPost(String newHttpPostUrl, String jsonData) throws IOException, ParseException { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(newHttpPostUrl); StringEntity requestEntity = new StringEntity(jsonData, ContentType.APPLICATION_JSON); post.setEntity(requestEntity);//from w ww . j ava2s .c o m HttpResponse response = client.execute(post); return response.getStatusLine().getStatusCode(); }
From source file:com.mber.client.HTTParty.java
private static Call execute(final HttpUriRequest request) throws IOException { HttpClient client = new DefaultHttpClient(); try {/*from ww w . j a va2 s . co m*/ request.addHeader("REST-API-Version", MBER_VERSION); HttpResponse response = client.execute(request); String body = toString(response.getEntity().getContent()); return new Call(request.getMethod(), request.getURI(), response.getStatusLine().getStatusCode(), body); } finally { client.getConnectionManager().shutdown(); } }
From source file:com.poguico.palmabici.network.synchronizer.NetworkSynchronizerTask.java
private static String[] getNetworkInfo() { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(URL); String[] ret = new String[2]; String line;//from w ww .j a va 2 s. c om try { HttpResponse response = client.execute(request); StatusLine statusLine = response.getStatusLine(); ret[0] = String.valueOf(statusLine.getStatusCode()); if (statusLine.getStatusCode() == HTTP_STATUS_OK) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); InputStreamReader contentReader = new InputStreamReader(content); BufferedReader reader = new BufferedReader(contentReader); while ((line = reader.readLine()) != null) { builder.append(line); } ret[1] = builder.toString(); } else { Log.e(NetworkSynchronizerTask.class.toString(), "Failed to download file"); } } catch (Exception e) { ret[0] = STR_HTTP_STATUS_NOT_FOUND; e.printStackTrace(); } return ret; }