List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:com.foundationdb.http.HttpMonitorVerifySSLIT.java
private static int openRestURL(HttpClient client, String userInfo, int port, String path) throws Exception { URI uri = new URI("https", 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()); return code;/*from w w w . ja v a 2s . com*/ }
From source file:in.goahead.apps.yaytd.util.URLUtils.java
public static InputStream OpenURL(String url, long skipLength) throws MalformedURLException, IOException { InputStream is = null;/* w w w .j av a2 s . c o m*/ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); if (skipLength > 0) { httpGet.setHeader("Range", "bytes=" + skipLength + "-"); } HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { is = httpEntity.getContent(); } return is; }
From source file:fr.pasteque.client.utils.URLTextGetter.java
public static void getBinary(final String url, final Map<String, String> getParams, final Handler h) { new Thread() { @Override/*from w w w . j a v a 2 s. c o m*/ public void run() { try { String fullUrl = url; if (getParams != null && getParams.size() > 0) { fullUrl += "?"; for (String param : getParams.keySet()) { fullUrl += URLEncoder.encode(param, "utf-8") + "=" + URLEncoder.encode(getParams.get(param), "utf-8") + "&"; } } if (fullUrl.endsWith("&")) { fullUrl = fullUrl.substring(0, fullUrl.length() - 1); } HttpClient client = new DefaultHttpClient(); HttpResponse response = null; HttpGet req = new HttpGet(fullUrl); response = client.execute(req); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { // Get http response byte[] content = null; try { final int size = 10240; ByteArrayOutputStream bos = new ByteArrayOutputStream(size); byte[] buffer = new byte[size]; BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(), size); int read = bis.read(buffer, 0, size); while (read != -1) { bos.write(buffer, 0, read); read = bis.read(buffer, 0, size); } content = bos.toByteArray(); } catch (IOException ioe) { ioe.printStackTrace(); } if (h != null) { Message m = h.obtainMessage(); m.what = SUCCESS; m.obj = content; m.sendToTarget(); } } else { if (h != null) { Message m = h.obtainMessage(); m.what = STATUS_NOK; m.obj = new Integer(status); m.sendToTarget(); } } } catch (IOException e) { if (h != null) { Message m = h.obtainMessage(); m.what = ERROR; m.obj = e; m.sendToTarget(); } } } }.start(); }
From source file:edu.uci.ics.hyracks.control.common.deployment.DeploymentUtils.java
/** * Download remote Http URLs and return the stored local file URLs * //from www . j a va 2s . c o m * @param urls * the remote Http URLs * @param deploymentDir * the deployment jar storage directory * @param isNC * true is NC/false is CC * @return a list of local file URLs * @throws HyracksException */ private static List<URL> downloadURLs(List<URL> urls, String deploymentDir, boolean isNC) throws HyracksException { //retry 10 times at maximum for downloading binaries int retryCount = 10; int tried = 0; Exception trace = null; while (tried < retryCount) { try { tried++; List<URL> downloadedFileURLs = new ArrayList<URL>(); File dir = new File(deploymentDir); if (!dir.exists()) { FileUtils.forceMkdir(dir); } for (URL url : urls) { String urlString = url.toString(); int slashIndex = urlString.lastIndexOf('/'); String fileName = urlString.substring(slashIndex + 1).split("&")[1]; String filePath = deploymentDir + File.separator + fileName; File targetFile = new File(filePath); if (isNC) { HttpClient hc = HttpClientBuilder.create().build(); HttpGet get = new HttpGet(url.toString()); HttpResponse response = hc.execute(get); InputStream is = response.getEntity().getContent(); OutputStream os = new FileOutputStream(targetFile); try { IOUtils.copyLarge(is, os); } finally { os.close(); is.close(); } } downloadedFileURLs.add(targetFile.toURI().toURL()); } return downloadedFileURLs; } catch (Exception e) { e.printStackTrace(); trace = e; } } throw new HyracksException(trace); }
From source file:com.balk.rsswidget.GoogleReaderHelper.java
/** * Pull the raw text content of the given URL. This call blocks until the * operation has completed, and is synchronized because it uses a shared * buffer {@link #sBuffer}.// w w w . jav a 2 s.co m * * @param url The exact URL to request. * @return The raw content returned by the server. * @throws ApiException If any connection or server error occurs. */ protected static synchronized String getUrlContent(String url) throws ApiException { if (sUserAgent == null) { throw new ApiException("User-Agent string must be prepared"); } // Create client and set our specific user-agent string HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); request.setHeader("User-Agent", sUserAgent); try { HttpResponse response = client.execute(request); // Check if server response is valid StatusLine status = response.getStatusLine(); if (status.getStatusCode() != HTTP_STATUS_OK) { throw new ApiException("Invalid response from server: " + status.toString()); } // Pull content stream from response HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ByteArrayOutputStream content = new ByteArrayOutputStream(); // Read response into a buffered stream int readBytes = 0; while ((readBytes = inputStream.read(sBuffer)) != -1) { content.write(sBuffer, 0, readBytes); } // Return result from buffered stream return new String(content.toByteArray()); } catch (IOException e) { throw new ApiException("Problem communicating with API", e); } }
From source file:org.keycloak.example.CustomerDatabaseClient.java
public static List<String> getCustomers(HttpServletRequest req) throws Failure { KeycloakSecurityContext session = (KeycloakSecurityContext) req .getAttribute(KeycloakSecurityContext.class.getName()); HttpClient client = new DefaultHttpClient(); try {//from w w w.j av a 2s . c o m HttpGet get = new HttpGet(UriUtils.getOrigin(req.getRequestURL().toString()) + "/database/customers"); get.addHeader("Authorization", "Bearer " + session.getTokenString()); try { HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new Failure(response.getStatusLine().getStatusCode()); } HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); try { return JsonSerialization.readValue(is, TypedList.class); } finally { is.close(); } } catch (IOException e) { throw new RuntimeException(e); } } finally { client.getConnectionManager().shutdown(); } }
From source file:org.jboss.additional.testsuite.jdkall.present.clustering.cluster.ejb.stateful.StatefulFailoverTestCase.java
private static int queryCount(HttpClient client, URI uri) throws IOException { HttpResponse response = client.execute(new HttpGet(uri)); try {/*from ww w . ja v a 2 s . com*/ assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); return Integer.parseInt(response.getFirstHeader("count").getValue()); } finally { HttpClientUtils.closeQuietly(response); } }
From source file:com.leprechaun.solveig.http.RequestExecutor.java
public static ResponseEntity simpleExecutor(String host, String port, String query) { ResponseEntity responseEntity = new ResponseEntity(); HttpClient client = new DefaultHttpClient(); String url = "http://" + host + ":" + port + "/query?q=" + query; System.out.println(url);/*from w ww. j a va2 s . com*/ HttpGet get = new HttpGet(url); try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseString); responseEntity.setCode(response.getStatusLine().toString()); responseEntity.setBody(responseString); } catch (IOException e) { System.out.println(e.getMessage()); responseEntity.setCode("ERROR"); responseEntity.setBody(e.getMessage()); } return responseEntity; }
From source file:com.photon.phresco.nativeapp.eshop.net.NetworkManager.java
public static boolean checkHttpURLStatus(final String url) { boolean httpStatusFlag = false; HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response;//from ww w.ja va 2s.co m try { response = httpclient.execute(httpget); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpURLConnection.HTTP_OK) { httpStatusFlag = true; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return httpStatusFlag; }
From source file:core.RESTCalls.RESTPost.java
public static String httpPost(String urlStr, String data) throws Exception { String result = null;/*w ww . j a v a2s . c o m*/ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urlStr); StringEntity input = new StringEntity(data); post.setEntity(input); HttpResponse response = client.execute(post); if (response != null && response.getEntity() != null) { BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder total = new StringBuilder(); String line = null; while ((line = r.readLine()) != null) total.append(line + "\n"); total.toString(); } return result; }