List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:org.wildfly.swarm.topology.consul.AdvertisingTestBase.java
protected Map<?, ?> getDefinedServicesAsMap() throws IOException { HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient client = builder.build(); HttpUriRequest request = new HttpGet(servicesUrl); CloseableHttpResponse response = client.execute(request); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); String content = EntityUtils.toString(response.getEntity()); return mapper.readValue(content, Map.class); }
From source file:com.mycompany.horus.ServiceListener.java
private String getWsdlServicos() throws IOException { String resp;/*w w w . j ava2s .c o m*/ CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(SERVICE_URL); HttpResponse response = httpclient.execute(post); HttpEntity respEntity = response.getEntity(); resp = EntityUtils.toString(respEntity); return resp; }
From source file:cn.aofeng.demo.httpclient.HttpClientBasic.java
public void get() throws URISyntaxException, ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet get = new HttpGet(_targetHost + "/get"); CloseableHttpResponse response = client.execute(get); processResponse(response);/*from w ww .ja va 2 s . c o m*/ }
From source file:com.clonephpscrapper.utility.FetchPageWithProxy.java
public static String fetchPageSourcefromClientGoogle(URI newurl) throws IOException, InterruptedException { int portNo = generateRandomPort(); CredentialsProvider credsprovider = new BasicCredentialsProvider(); credsprovider.setCredentials(new AuthScope("195.154.161.103", portNo), new UsernamePasswordCredentials("mongoose", "Fjh30fi")); HttpHost proxy = new HttpHost("195.154.161.103", portNo); //----------------------------------------------------------------------- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider) .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0") .setDefaultRequestConfig(requestConfig).setProxy(proxy).build(); String responsebody = ""; String responsestatus = null; int count = 0; try {/*from www .java 2 s .c o m*/ HttpGet httpget = new HttpGet(newurl); httpget.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.addHeader("Accept-Encoding", "gzip, deflate"); httpget.addHeader("Accept-Language", "en-US,en;q=0.5"); httpget.addHeader("Connection", "keep-alive"); System.out.println("Response status " + httpget.getRequestLine()); CloseableHttpResponse resp = httpclient.execute(httpget); responsestatus = resp.getStatusLine().toString(); if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403") || responsestatus.contains("400") || responsestatus.contains("407") || responsestatus.contains("401") || responsestatus.contains("402") || responsestatus.contains("404") || responsestatus.contains("405") || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999") || responsestatus.contains("ClientProtocolException") || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) { Thread.sleep(10000); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl); if (responsebody == null) { Thread.sleep(10000); System.out.println("PROX FAILURE"); } if (count > 20) { Thread.sleep(1000); break; } } while (responsebody == null || "".equals(responsebody)); } else { HttpEntity entity = resp.getEntity(); System.out.println(resp.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { responsebody = new StringBuilder().append(responsebody).append(inputLine).toString(); } // writeResponseFile(responsebody, pagename); } EntityUtils.consume(entity); } } catch (IOException | IllegalStateException e) { System.out.println("Exception = " + e); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl); if (responsebody == null) { System.out.println("PROX FAILURE"); } if (count > 15) { Thread.sleep(50000); // responsebody = fetchPageSourcefromClientGoogleSecond(newurl); break; } } while (responsebody == null || "".equals(responsebody)); } finally { httpclient.close(); } return responsebody; }
From source file:com.ibm.mil.readyapps.telco.adapters.CloudantGeoAdapterResource.java
@GET @Produces(MediaType.APPLICATION_JSON)/* w w w .j av a 2 s . c o m*/ @Path("/{user_id}/wifi") public Response getWifiLocations(@PathParam("user_id") String name, @QueryParam(value = "lat") double latitude, @QueryParam(value = "lon") double longitude) { GeoJsonPoint userLocation = new GeoJsonPoint(latitude, longitude); try { HttpGet httpget = new HttpGet(GeoRadiusURI.build(DEMO_LOCATION)); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse httpResponse = httpClient.execute(httpget); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.OK.getCode()) { String jsonString = EntityUtils.toString(httpResponse.getEntity()); List<WifiHotspotFlat> hotspots = WifiHotspotUtils.parseAndOffsetHotspots(jsonString, userLocation); return Response.ok(new Gson().toJson(hotspots), MediaType.APPLICATION_JSON).build(); } httpClient.close(); return Response.serverError().entity(httpResponse.getStatusLine()).build(); } catch (URISyntaxException | ParseException | IOException e) { e.printStackTrace(); return Response.serverError().entity("Error").build(); } }
From source file:com.mycompany.horus.ServiceListener.java
private String getWsdlParametros() throws IOException { String resp;/*w w w. j a va 2 s . c o m*/ CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(PARAMETERS_URL); HttpResponse response = httpclient.execute(post); HttpEntity respEntity = response.getEntity(); resp = EntityUtils.toString(respEntity); return resp; }
From source file:org.epics.archiverappliance.utils.ui.GetUrlContent.java
/** * Post a JSONObject to a remote server and get the response as a JSON object. * @param url URL/*w w w. j a v a 2 s . c o m*/ * @param object A JSONObject * @return JSONObject   * @throws IOException   */ public static JSONObject postObjectAndGetContentAsJSONObject(String url, JSONObject object) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost postMethod = new HttpPost(url); postMethod.addHeader(ARCHAPPL_COMPONENT, "true"); postMethod.addHeader("Content-Type", MimeTypeConstants.APPLICATION_JSON); postMethod.addHeader("Connection", "close"); // https://www.nuxeo.com/blog/using-httpclient-properly-avoid-closewait-tcp-connections/ StringEntity archiverValues = new StringEntity(JSONValue.toJSONString(object), ContentType.APPLICATION_JSON); postMethod.setEntity(archiverValues); if (logger.isDebugEnabled()) { logger.debug("About to make a POST with " + url); } HttpResponse response = httpclient.execute(postMethod); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("Obtained a HTTP entity of length " + entity.getContentLength()); // ArchiverValuesHandler takes over the burden of closing the input stream. try (InputStream is = entity.getContent()) { JSONObject retval = (JSONObject) JSONValue.parse(new InputStreamReader(is)); return retval; } } else { throw new IOException("HTTP response did not have an entity associated with it"); } }
From source file:com.yahoo.gondola.container.LocalTestRoutingServerTest.java
@Test public void testRoutingServer() throws Exception { server = new LocalTestRoutingServer(gondola, routingHelper, proxyClientProvider, new HashMap<String, RoutingService>() { {/*from w ww. j a v a 2 s . c o m*/ put("shard1", routingService); put("shard2", routingService); } }, changeLogProcessor); CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(new HttpGet(server.getHostUri())); assertEquals(response.getStatusLine().getStatusCode(), 503); assertEquals(EntityUtils.toString(response.getEntity()), "No leader is available"); }