List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:com.netflix.explorers.helloworld.ExplorerAppTest.java
@Test public void verifyRESTEndpoints() throws IOException { HttpClient client = new DefaultHttpClient(); for (Map.Entry<String, String> restEndPoint : REST_END_POINTS.entrySet()) { final String endPoint = buildLocalHostEndpoint(restEndPoint.getKey()); LOG.info("REST endpoint " + endPoint); HttpGet restGet = new HttpGet(endPoint); HttpResponse response = client.execute(restGet); assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(restEndPoint.getValue(), response.getEntity().getContentType().getValue()); // need to consume full response before make another rest call with // the default SingleClientConnManager used with DefaultHttpClient EntityUtils.consume(response.getEntity()); }/*from w w w .j av a 2 s .com*/ }
From source file:de.adesso.referencer.search.helper.ElasticConfig.java
public static String sendHttpRequest(String url, String requestBody, String requestType) throws IOException { String result = null;//w w w.jav a2 s. com CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse httpResponse; try { switch (requestType) { case "Get": httpResponse = httpclient.execute(new HttpGet(url)); break; case "Post": HttpPost httppost = new HttpPost(url); if (requestBody != null) httppost.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); httpResponse = httpclient.execute(httppost); break; case "Put": HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json"); httpPut.addHeader("Accept", "application/json"); if (requestBody != null) httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); httpResponse = httpclient.execute(httpPut); break; case "Delete": httpResponse = httpclient.execute(new HttpDelete(url)); break; default: httpResponse = httpclient.execute(new HttpGet(url)); break; } try { HttpEntity entity1 = httpResponse.getEntity(); if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < MAX_CONTENT_LENGTH) { result = EntityUtils.toString(entity1, DEFAULT_CHARSET); } else { System.out.println("Error!!!! entity length=" + len); } } EntityUtils.consume(entity1); } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } return result; }
From source file:com.threatconnect.sdk.conn.HttpRequestExecutor.java
@Override public String execute(String path, HttpMethod type, Object obj) throws IOException { path += (path.contains("?") ? "&" : "?"); path += "createActivityLog=" + this.conn.getConfig().isActivityLogEnabled(); logger.trace("Path: " + path); String fullPath = this.conn.getConfig().getTcApiUrl() + path.replace("/api/", "/"); logger.trace("Full: " + type + ": " + fullPath); HttpRequestBase httpBase = getBase(fullPath, type); if (obj != null) applyEntityAsJSON(httpBase, obj); logger.trace("RawPath: " + httpBase.getURI().getPath()); logger.trace("Query: " + httpBase.getURI().getRawQuery()); logger.trace("Path: " + path); String headerPath = httpBase.getURI().getRawPath() + "?" + httpBase.getURI().getRawQuery(); logger.trace("HeaderPath: " + headerPath); ConnectionUtil.applyHeaders(this.conn.getConfig(), httpBase, type.toString(), headerPath); logger.trace("Request: " + httpBase.getRequestLine()); long startMs = System.currentTimeMillis(); CloseableHttpResponse response = this.conn.getApiClient().execute(httpBase); notifyListeners(type, fullPath, (System.currentTimeMillis() - startMs)); String result = null;//from w w w . ja va2s .co m try { logger.trace(response.getStatusLine().toString()); HttpEntity entity = response.getEntity(); logger.trace("Response Headers: " + Arrays.toString(response.getAllHeaders())); logger.trace("Content Encoding: " + entity.getContentEncoding()); if (entity != null) { result = EntityUtils.toString(entity, StandardCharsets.UTF_8); logger.trace("Result:" + result); EntityUtils.consume(entity); } } finally { response.close(); } return result; }
From source file:com.ibm.streamsx.topology.internal.streaminganalytics.RestUtils.java
public static JsonObject getGsonResponse(CloseableHttpClient httpClient, HttpRequestBase request) throws IOException, ClientProtocolException { request.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()); CloseableHttpResponse response = httpClient.execute(request); JsonObject jsonResponse;/*from w w w .ja v a 2s .c o m*/ try { HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final String errorInfo; if (entity != null) errorInfo = " -- " + EntityUtils.toString(entity); else errorInfo = ""; throw new IllegalStateException( "Unexpected HTTP resource from service:" + response.getStatusLine().getStatusCode() + ":" + response.getStatusLine().getReasonPhrase() + errorInfo); } if (entity == null) throw new IllegalStateException("No HTTP resource from service"); Reader r = new InputStreamReader(entity.getContent()); jsonResponse = new Gson().fromJson(r, JsonObject.class); EntityUtils.consume(entity); } finally { response.close(); } return jsonResponse; }
From source file:org.cloudcrawler.domain.crawler.robotstxt.RobotsTxtService.java
/** * This method is used to evaluate if the provided uri is * allowed to be crawled against the robots.txt of the website. * * @param uri// www. jav a 2 s . c o m * @return * @throws Exception */ public boolean isAllowedUri(URI uri) throws Exception { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme(uri.getScheme()); uriBuilder.setHost(uri.getHost()); uriBuilder.setUserInfo(uri.getUserInfo()); uriBuilder.setPath("/robots.txt"); URI robotsTxtUri = uriBuilder.build(); BaseRobotRules rules = (BaseRobotRules) cache.get(robotsTxtUri.toString()); if (rules == null) { HttpResponse response = httpService.get(robotsTxtUri); try { // HACK! DANGER! Some sites will redirect the request to the top-level domain // page, without returning a 404. So look for a response which has a redirect, // and the fetched content is not plain text, and assume it's one of these... // which is the same as not having a robotstxt.txt file. String contentType = response.getEntity().getContentType().getValue(); boolean isPlainText = (contentType != null) && (contentType.startsWith("text/plain")); if (response.getStatusLine().getStatusCode() == 404 || !isPlainText) { rules = robotsTxtParser.failedFetch(HttpStatus.SC_GONE); } else { StringWriter writer = new StringWriter(); IOUtils.copy(response.getEntity().getContent(), writer); rules = robotsTxtParser.parseContent(uri.toString(), writer.toString().getBytes(), response.getEntity().getContentType().getValue(), httpService.getUserAgent()); } } catch (Exception e) { EntityUtils.consume(response.getEntity()); throw e; } EntityUtils.consume(response.getEntity()); cache.set(robotsTxtUri.toString(), 60 * 60 * 24, rules); } return rules.isAllowed(uri.toString()); }
From source file:com.mywork.framework.util.RemoteHttpUtil.java
/** * ?? Post/* ww w . ja v a2 s . c o m*/ * @return */ public static String doPost(String contentUrl, Map<String, String> headerMap, String jsonBody) { String result = null; CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost post = new HttpPost(contentUrl); RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT_SECONDS * 1000) .setConnectTimeout(TIMEOUT_SECONDS * 1000).setSocketTimeout(TIMEOUT_SECONDS * 1000).build(); post.setConfig(config); try { if (headerMap != null && !headerMap.isEmpty()) { for (Map.Entry<String, String> m : headerMap.entrySet()) { post.setHeader(m.getKey(), m.getValue()); } } if (jsonBody != null) { StringEntity entity = new StringEntity(jsonBody, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); } long start = System.currentTimeMillis(); response = httpClient.execute(post); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); logger.info("url = " + contentUrl + " request spend time = " + (System.currentTimeMillis() - start)); EntityUtils.consume(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:hu.dolphio.tprttapi.service.RttServiceImpl.java
@Override public Collection<ReportElementTO> loadRttTrackingsFromReport() throws URISyntaxException, IOException, ClientException { if ((projectId == null && reportId == null) || (projectId != null && reportId != null)) { throw new ClientException("Project ID or Report ID must be set!"); }/*from w w w .j a v a2 s . co m*/ BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore) .setDefaultRequestConfig(config).build(); HttpEntity httpEntity = new InputStreamEntity(new ByteArrayInputStream( new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new Object() { public String username = propertyReader.getRttUserName(); public String password = propertyReader.getRttPassword(); }).getBytes("UTF-8"))); HttpUriRequest login = RequestBuilder.post().setUri(new URI(propertyReader.getRttHost() + "/login")) .setEntity(httpEntity).setHeader("Accept-Language", "sk,en-US;q=0.8,en;q=0.6,hu;q=0.4") .setHeader("Content-Type", "application/json;charset=utf-8").build(); CloseableHttpResponse loginResponse = httpclient.execute(login); LOG.debug("RTT login response: " + loginResponse); if (HttpResponseStatus .getStatusByCode(loginResponse.getStatusLine().getStatusCode()) != HttpResponseStatus.SUCCESS) { throw new ClientException( "[" + loginResponse.getStatusLine().getStatusCode() + "] Login to RTT failed!"); } EntityUtils.consume(loginResponse.getEntity()); StringBuilder postUriBuilder = new StringBuilder().append(propertyReader.getRttHost()) .append(reportId == null ? propertyReader.getRttReportByProjectUrl() : propertyReader.getRttReportByReportUrl()) .append(reportId == null ? projectId : reportId).append("/json?startDate=").append(dateFrom) .append("&endDate=").append(dateTo); LOG.trace("RTT report query: " + postUriBuilder.toString()); HttpGet get = new HttpGet(postUriBuilder.toString()); CloseableHttpResponse rttResponse = httpclient.execute(get); if (HttpResponseStatus .getStatusByCode(rttResponse.getStatusLine().getStatusCode()) != HttpResponseStatus.SUCCESS) { throw new ClientException("[" + rttResponse.getStatusLine().getStatusCode() + "] Downloading tracking information from RTT failed!"); } String trackingsJson = IOUtils.toString(rttResponse.getEntity().getContent(), "utf-8"); Collection<ReportElementTO> fromJson = new ObjectMapper().readValue(trackingsJson, new TypeReference<Collection<ReportElementTO>>() { }); return fromJson; }
From source file:amazonechoapi.AmazonEchoApi.java
public boolean httpLogin() { try {// w ww .ja v a 2 s .c om String output = httpGet(""); Document doc = Jsoup.parse(output); Elements forms = doc.select("form"); String action = forms.attr("action"); if (action.isEmpty()) { return false; } Elements hidden = doc.select("input[type=hidden]"); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("email", USERNAME)); nvps.add(new BasicNameValuePair("password", PASSWORd)); nvps.add(new BasicNameValuePair("create", "0")); for (Element el1 : hidden) { nvps.add(new BasicNameValuePair(el1.attr("name"), el1.attr("value"))); } HttpPost httpPost = new HttpPost(action); httpPost.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13"); httpPost.setHeader(HttpHeaders.REFERER, BASE_URL); httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse httpResponse = httpclient.execute(httpPost); httpResponse.getEntity(); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { EntityUtils.consume(entity); } System.out.println("Login successful"); return true; } catch (Exception e) { System.out.println("Login Error:" + e.getMessage()); return false; } }
From source file:com.basho.riak.client.http.response.DefaultHttpResponse.java
public void close() { if (httpResponse != null) { try {/* w w w . j a v a 2 s .c om*/ EntityUtils.consume(httpResponse.getEntity()); } catch (IOException e) { throw new RiakIORuntimeException(e); } } }
From source file:org.wuspba.ctams.ws.ITBandMemberController.java
@Test public void testList() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); LOG.info("Connecting to " + uri.toString()); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getBandMembers().size(), 3); for (BandMember m : doc.getBandMembers()) { if (m.getId().equals(TestFixture.INSTANCE.andyMember.getId())) { testEquality(m, TestFixture.INSTANCE.andyMember); } else if (m.getId().equals(TestFixture.INSTANCE.bobMember.getId())) { testEquality(m, TestFixture.INSTANCE.bobMember); } else if (m.getId().equals(TestFixture.INSTANCE.jamieMember.getId())) { testEquality(m, TestFixture.INSTANCE.jamieMember); } else { fail();/*from w w w .ja va 2 s.c om*/ } } EntityUtils.consume(entity); } }