List of usage examples for org.apache.http.client.methods CloseableHttpResponse getEntity
HttpEntity getEntity();
From source file:jchat.test.RestTest.java
private String getBody(CloseableHttpResponse response) throws Exception { String body = null;/*from ww w . j a v a 2s . co m*/ try { HttpEntity entity = response.getEntity(); body = EntityUtils.toString(entity); EntityUtils.consume(entity); } finally { response.close(); } return body; }
From source file:co.paralleluniverse.comsat.webactors.AbstractWebActorTest.java
@Test public void testHttpRedirect() throws IOException, InterruptedException, ExecutionException { final HttpGet httpGet = new HttpGet("http://localhost:8080/redirect"); try (final CloseableHttpClient client = HttpClients.custom().disableRedirectHandling() .setDefaultRequestConfig(requestConfig).build()) { final CloseableHttpResponse res = client.execute(httpGet); final String s = EntityUtils.toString(res.getEntity()); System.out.println(s);// ww w .j ava2 s.c o m assertEquals(302, res.getStatusLine().getStatusCode()); assertTrue(res.getFirstHeader("Location").getValue().endsWith("/foo")); } }
From source file:com.srotya.tau.ui.alerts.AlertReceiver.java
/** * @param ruleId/*w ww . j a v a 2s . c o m*/ * @return */ @SuppressWarnings("unchecked") public Queue<Map<String, Object>> getChannel(short ruleId) throws Exception { CloseableHttpClient client = Utils.buildClient(am.getBaseUrl(), am.getConnectTimeout(), am.getRequestTimeout()); HttpGet get = new HttpGet(am.getBaseUrl() + "/receive/events/" + ruleId); CloseableHttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() < 300) { String result = EntityUtils.toString(response.getEntity()); Gson gson = new Gson(); Queue<Map<String, Object>> queue = gson.fromJson(result, Queue.class); return queue; } return new LinkedList<>(); }
From source file:HelloWorld.HelloWorldTest.java
@Test public void getPeopleNotExist() throws URISyntaxException, IOException { URI uri = new URI(baseURI + "api/person/sally"); HttpGet get = new HttpGet(uri); CloseableHttpResponse response = client.execute(get); assertEquals(404, response.getStatusLine().getStatusCode()); assertEquals("No person exists with specified name.", EntityUtils.toString(response.getEntity(), "UTF-8")); }
From source file:net.xenqtt.httpgateway.HttpGatewayHandler.java
/** * The handler sets the response status, content-type, and marks the request as handled before it generates the body of the response using a writer. There * are a number of built in <a href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/server/handler/package-summary.html">handlers</a> * that might come in handy./*from w w w . j a v a 2 s .c o m*/ * * @param target * the target of the request, which is either a URI or a name from a named dispatcher. * @param baseRequest * the Jetty mutable request object, which is always unwrapped. * @param request * the immutable request object, which may have been wrapped by a filter or servlet. * @param response * the response, which may have been wrapped by a filter or servlet. * * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // FIXME [jim] - make sure http 1.1 persistent connections are being used on both server and client sides CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.google.com" + target); CloseableHttpResponse response1 = httpclient.execute(httpGet); try { response.setStatus(response1.getStatusLine().getStatusCode()); HttpEntity entity = response1.getEntity(); InputStream in = entity.getContent(); OutputStream out = response.getOutputStream(); for (int i = in.read(); i >= 0; i = in.read()) { out.write(i); } } finally { response1.close(); } baseRequest.setHandled(true); // TODO Auto-generated method stub }
From source file:com.artisan.hivemq.callbacks.AuthenticationCallback.java
@Override @Cached(timeToLive = 300, timeUnit = TimeUnit.SECONDS) public Boolean checkCredentials(final ClientCredentialsData clientCredentialsData) throws AuthenticationException { /*TODO: In a production ready plugin you should cache here if possible!! You can use hte @Cache Annotation for that or implement caching yourself */ final HttpPost httpPost = new HttpPost(MOCK_HTTP_SERVICE); CloseableHttpResponse response = null; try {/*from w w w . j a v a2s. c o m*/ response = httpClient.execute(httpPost); final HttpEntity entity = response.getEntity(); if (entity != null) { final HashMap result = objectMapper.readValue(EntityUtils.toString(entity), HashMap.class); if ("valid".equals(result.get("auth"))) { return true; } } } catch (JsonParseException e) { log.error("Error while parsing the results from the webservice call", e); } catch (JsonMappingException e) { log.error("Error while parsing the results from the webservice call", e); } catch (ClientProtocolException e) { log.error("There was an error in the HTTP communication with the server", e); } catch (IOException e) { log.error("Could not get valid results from the webservice", e); } finally { closeGracefully(response); } //If we're getting here, we didn't get a valid response from the webservice return false; }
From source file:webrequester.AbstractWebRequest.java
public String requestWithGet() { String s = ""; try {/*w w w. j av a 2s . co m*/ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(buildURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); if (entity != null) { s = EntityUtils.toString(entity, "UTF-8"); } } finally { response.close(); } } catch (IOException ex) { return null; } return s; }
From source file:org.sikuli.script.App.java
/** * issue a http(s) request// ww w .j av a2 s . c o m * @param url a valid url as used in a browser * @return textual content of the response or empty (UTF-8) * @throws IOException */ public static String wwwGet(String url) throws IOException { HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; ResponseHandler rh = new ResponseHandler() { @Override public String handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response has no content"); } InputStream is = entity.getContent(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } }; boolean oneTime = false; if (httpclient == null) { wwwStart(); oneTime = true; } Object content = httpclient.execute(httpget, rh); if (oneTime) { wwwStop(); } return (String) content; }
From source file:io.liveoak.filesystem.HTTPFilesystemResourceTest.java
@Test public void testReadChild() throws Exception { HttpGet get = new HttpGet("http://localhost:8080/testApp/files/test-file1.txt"); get.addHeader("Accept", "text/*"); try {/*from w w w .j a va2s. c om*/ System.err.println("DO GET"); CloseableHttpResponse result = httpClient.execute(get); System.err.println("=============>>>"); System.err.println(result); HttpEntity entity = result.getEntity(); if (entity.getContentLength() > 0) { entity.writeTo(System.err); } System.err.println("\n<<<============="); assertThat(result.getStatusLine().getStatusCode()).isEqualTo(200); } finally { httpClient.close(); } }
From source file:io.liveoak.filesystem.HTTPFilesystemResourceTest.java
@Test public void testEnumerateRoot() throws Exception { HttpGet get = new HttpGet("http://localhost:8080/testApp/files"); get.addHeader("Accept", "application/json"); try {//from www. j a va 2s . co m System.err.println("DO GET"); CloseableHttpResponse result = httpClient.execute(get); System.err.println("=============>>>"); System.err.println(result); HttpEntity entity = result.getEntity(); if (entity.getContentLength() > 0) { entity.writeTo(System.err); } System.err.println("\n<<<============="); assertThat(result.getStatusLine().getStatusCode()).isEqualTo(200); } finally { httpClient.close(); } }