List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:nayan.netty.client.FileUploadClient.java
private static void uploadFile() throws Exception { File file = new File("small.jpg"); HttpEntity httpEntity = MultipartEntityBuilder.create() .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName()).build(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("http://localhost:8080"); httppost.setEntity(httpEntity);//from ww w.j a v a2s . com System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); response.close(); }
From source file:com.vmware.content.samples.client.util.HttpUtil.java
/** * Uploads a file from local storage to a given HTTP URI. * * @param localFile local storage path to the file to upload. * @param uploadUri HTTP URI where the file needs to be uploaded. * @throws java.security.NoSuchAlgorithmException * @throws java.security.KeyStoreException * @throws java.security.KeyManagementException * @throws java.io.IOException/*from www . j a va2s . co m*/ */ public static void uploadFileToUri(File localFile, URI uploadUri) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { CloseableHttpClient httpClient = getCloseableHttpClient(); HttpPut request = new HttpPut(uploadUri); HttpEntity content = new FileEntity(localFile); request.setEntity(content); HttpResponse response = httpClient.execute(request); EntityUtils.consumeQuietly(response.getEntity()); }
From source file:net.wedjaa.elasticparser.tester.ESSearchTester.java
private static int countIndexed() { System.out.println("Getting the indexing status"); int indexed = 0; HttpGet statusGet = new HttpGet("http://localhost:9500/unit/_stats"); try {/*from w w w . j a va 2 s .c o m*/ CloseableHttpClient httpclient = HttpClientBuilder.create().build(); System.out.println("Executing request"); HttpResponse response = httpclient.execute(statusGet); System.out.println("Processing response"); InputStream isResponse = response.getEntity().getContent(); BufferedReader isReader = new BufferedReader(new InputStreamReader(isResponse)); StringBuilder strBuilder = new StringBuilder(); String readLine; while ((readLine = isReader.readLine()) != null) { strBuilder.append(readLine); } isReader.close(); System.out.println("Done - reading JSON"); JSONObject esResponse = new JSONObject(strBuilder.toString()); indexed = esResponse.getJSONObject("indices").getJSONObject("unit").getJSONObject("total") .getJSONObject("docs").getInt("count"); System.out.println("Indexed docs: " + indexed); httpclient.close(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } return indexed; }
From source file:com.github.tmyroadctfig.icloud4j.util.ICloudUtils.java
/** * Parses a JSON response from the request. * * @param httpClient the HTTP client.// w w w .j av a 2 s . c om * @param post the request. * @param responseClass the type of JSON object to parse the values into. * @param <T> the type to parse into. * @return the object. * @throws ICloudException if there was an error returned from the request. */ public static <T> T parseJsonResponse(CloseableHttpClient httpClient, HttpPost post, Class<T> responseClass) { String rawResponseContent = "<no content>"; try (CloseableHttpResponse response = httpClient.execute(post)) { rawResponseContent = new StringResponseHandler().handleResponse(response); try { return fromJson(rawResponseContent, responseClass); } catch (JsonSyntaxException e1) { Map<String, Object> errorMap = fromJson(rawResponseContent, Map.class); System.err.println(rawResponseContent); throw new ICloudException(response, errorMap); } } catch (IOException e) { System.err.println(rawResponseContent); throw new UncheckedIOException(e); } }
From source file:org.fcrepo.camel.indexing.triplestore.integration.TestUtils.java
/** * Post data to the provided URL//ww w . j a va2 s . c om */ public static void httpPost(final String url, final String content, final String mimeType) throws Exception { final CloseableHttpClient httpClient = HttpClients.createDefault(); final HttpPost post = new HttpPost(url); post.addHeader(Exchange.CONTENT_TYPE, mimeType); post.setEntity(new StringEntity(content)); httpClient.execute(post); }
From source file:cz.incad.kramerius.utils.solr.SolrUtils.java
public static InputStream getSolrDataInternal(String query, String format) throws IOException { String solrHost = KConfiguration.getInstance().getSolrHost(); String uri = solrHost + "/select?" + query; if (!uri.endsWith("&")) { uri = uri + "&wt=" + format; } else {/* ww w . java 2s . c o m*/ uri = uri + "wt=" + format; } HttpGet httpGet = new HttpGet(uri); CloseableHttpClient client = HttpClients.createDefault(); HttpResponse response = client.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { return response.getEntity().getContent(); } else { throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); } }
From source file:com.google.appengine.tck.byteman.ConcurrentTxTestBase.java
protected static Thread execute(final CloseableHttpClient client, final HttpUriRequest request, final Holder holder) { Thread thread = new Thread(new Runnable() { public void run() { try { try (CloseableHttpResponse response = client.execute(request)) { holder.out = EntityUtils.toString(response.getEntity()); }// w w w . j a va 2 s.c om } catch (IOException ignore) { } } }); thread.start(); return thread; }
From source file:org.opennms.poller.remote.MetadataUtils.java
public static Map<String, String> fetchGeodata() { final Map<String, String> ret = new HashMap<>(); final String url = "http://freegeoip.net/xml/"; final CloseableHttpClient httpclient = HttpClients.createDefault(); final HttpGet get = new HttpGet(url); CloseableHttpResponse response = null; try {//from w ww .ja v a 2 s.co m response = httpclient.execute(get); final HttpEntity entity = response.getEntity(); final String xml = EntityUtils.toString(entity); System.err.println("xml = " + xml); final GeodataResponse geoResponse = JaxbUtils.unmarshal(GeodataResponse.class, xml); ret.put("external-ip-address", InetAddressUtils.str(geoResponse.getIp())); ret.put("country-code", geoResponse.getCountryCode()); ret.put("region-code", geoResponse.getRegionCode()); ret.put("city", geoResponse.getCity()); ret.put("zip-code", geoResponse.getZipCode()); ret.put("time-zone", geoResponse.getTimeZone()); ret.put("latitude", geoResponse.getLatitude() == null ? null : geoResponse.getLatitude().toString()); ret.put("longitude", geoResponse.getLongitude() == null ? null : geoResponse.getLongitude().toString()); EntityUtils.consumeQuietly(entity); } catch (final Exception e) { LOG.debug("Failed to get GeoIP data from " + url, e); } finally { IOUtils.closeQuietly(response); } return ret; }
From source file:com.gogh.plugin.translator.TranslatorUtil.java
public static String fetchInfo(String query, TranslatorEx translator) { final CloseableHttpClient client = TranslatorUtil.createClient(); try {/*from w ww.ja v a2 s . c om*/ final URI queryUrl = translator.createUrl(query); HttpGet httpGet = new HttpGet(queryUrl); HttpResponse response = client.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity resEntity = response.getEntity(); return translator.generateSuccess(resEntity); } else { return translator.generateFail(response); } } catch (Exception ignore) { ignore.printStackTrace(); } finally { try { client.close(); } catch (IOException ignore) { } } return null; }
From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java
public static void sendPut(String serverURL, String postBody) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); HttpPut post = new HttpPut(serverURL); StringEntity entity = new StringEntity(postBody, "UTF-8"); post.setEntity(entity);/*from www .j av a 2 s .com*/ HttpResponse response = client.execute(post); // System.out.println("\nSending 'PUT' request to URL : " + serverURL); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } client.close(); // System.out.println(result.toString()); }