List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:utils.HttpComm.java
public static String schedulerPost(String workerURL, Map<String, String> postArguments) throws Exception { HttpPost httpPost = new HttpPost(workerURL); httpPost.setProtocolVersion(HttpVersion.HTTP_1_1); List<NameValuePair> nvps = new ArrayList<>(); for (String key : postArguments.keySet()) nvps.add(new BasicNameValuePair(key, postArguments.get(key))); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { //System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); String s = EntityUtils.toString(entity); EntityUtils.consume(entity); return s; } finally {//from ww w .jav a 2s .co m httpPost.releaseConnection(); } }
From source file:net.joala.expression.library.net.UriStatusCodeExpression.java
@Override @Nonnull/* w w w . j av a 2 s .c o m*/ public Integer get() { final String host = uri.getHost(); checkState(knownHost().matches(host), "Host %s from URI %s is unknown.", host, uri); final DefaultHttpClient httpClient = new DefaultHttpClient(); try { final HttpParams httpParams = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, (int) timeout.in(TimeUnit.MILLISECONDS)); HttpConnectionParams.setSoTimeout(httpParams, (int) timeout.in(TimeUnit.MILLISECONDS)); final HttpUriRequest httpHead = new HttpHead(uri); try { final HttpResponse response = httpClient.execute(httpHead); final HttpEntity httpEntity = response.getEntity(); final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); if (httpEntity != null) { EntityUtils.consume(httpEntity); } return statusCode; } catch (IOException e) { throw new ExpressionEvaluationException(format("Failure reading from URI %s.", uri), e); } } finally { httpClient.getConnectionManager().shutdown(); } }
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 {// w w w.j a va2 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.sixsq.slipstream.DeploymentController.java
public URI runModule(String module) throws MojoExecutionException { try {//from w ww . j a v a 2 s .c om URI postUri = relativeURI("run"); HttpPost httppost = new HttpPost(postUri); List<NameValuePair> formInfo = new ArrayList<NameValuePair>(); formInfo.add(new BasicNameValuePair("refqname", module)); httppost.setEntity(new UrlEncodedFormEntity(formInfo, HTTP.UTF_8)); HttpResponse response = client.execute(httppost); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); Header location = response.getFirstHeader("location"); if (location == null) { throw new MojoExecutionException("running module failed; no redirect given"); } URI runUri = new URI(location.getValue()); return runUri; } catch (IOException e) { throw new MojoExecutionException("IO exception when trying to contact server", e); } catch (URISyntaxException e) { throw new MojoExecutionException("invalid URI syntax", e); } }
From source file:org.getwheat.harvest.library.RequestProcessor.java
/** * Processes the Request.// www .j av a2 s.c o m * * @param request the Request * @param credentials the User Credentials for this Request * @throws ClientProtocolException * @throws IOException * @throws IllegalStateException * @throws ParserConfigurationException * @throws SAXException * @throws URISyntaxException * @throws ThrottleException if too many requests are made to the Harvest Timer API * @throws UnexpectedHttpStatusException if an expected HTTP Status Code is encountered */ public void process(final Request<?> request, final UserCredentials credentials) throws ClientProtocolException, IOException, IllegalStateException, ParserConfigurationException, SAXException, URISyntaxException, ThrottleException, UnexpectedHttpStatusException { HttpEntity entity = null; try { request.clearResults(); final HttpClient client = manager.getHttpClient(); final HttpResponse response = client.execute(buildRequest(request, credentials)); if (!request.getHttpMethod().isExpectedHttpStatus(response.getStatusLine().getStatusCode())) { validateHttpStatus(response.getStatusLine(), response.getHeaders(VALUE_RETRY_AFTER)); } entity = response.getEntity(); if (entity != null) { request.process(response, entity); } } finally { EntityUtils.consume(entity); } }
From source file:org.eclipse.lyo.testsuite.oslcv2.SimplifiedQueryXmlTests.java
protected void validateNonEmptyResponse(String query) throws XPathExpressionException, IOException, ParserConfigurationException, SAXException { String queryUrl = OSLCUtils.addQueryStringToURL(currentUrl, query); HttpResponse response = OSLCUtils.getResponseFromUrl(setupBaseUrl, queryUrl, basicCreds, OSLCConstants.CT_XML, headers); int statusCode = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK != statusCode) { EntityUtils.consume(response.getEntity()); throw new IOException("Response code: " + statusCode + " for " + queryUrl); }//w w w.j a v a2 s . c o m String responseBody = EntityUtils.toString(response.getEntity()); Document doc = OSLCUtils.createXMLDocFromResponseBody(responseBody); Node results = (Node) OSLCUtils.getXPath().evaluate("//oslc:ResponseInfo/@rdf:about", doc, XPathConstants.NODE); // Only test oslc:ResponseInfo if found if (results != null) { assertEquals("Expended ResponseInfo/@rdf:about to equal request URL", queryUrl, results.getNodeValue()); results = (Node) OSLCUtils.getXPath().evaluate("//oslc:totalCount", doc, XPathConstants.NODE); if (results != null) { int totalCount = Integer.parseInt(results.getTextContent()); assertTrue("Expected oslc:totalCount > 0", totalCount > 0); } NodeList resultList = (NodeList) OSLCUtils.getXPath().evaluate("//rdf:Description/rdfs:member", doc, XPathConstants.NODESET); assertNotNull("Expected rdfs:member(s)", resultList); assertNotNull("Expected > 1 rdfs:member(s)", resultList.getLength() > 0); } }
From source file:org.jboss.as.test.integration.security.loginmodules.AbstractLoginModuleTest.java
protected HttpResponse authAndGetResponse(String URL, String user, String pass) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response;/*from www .j a v a 2s . c om*/ HttpGet httpget = new HttpGet(URL); response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) EntityUtils.consume(entity); // We should get the Login Page StatusLine statusLine = response.getStatusLine(); System.out.println("Login form get: " + statusLine); assertEquals(200, statusLine.getStatusCode()); System.out.println("Initial set of cookies:"); List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } // We should now login with the user name and password HttpPost httpost = new HttpPost(URL + "/j_security_check"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("j_username", user)); nvps.add(new BasicNameValuePair("j_password", pass)); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = httpclient.execute(httpost); int statusCode = response.getStatusLine().getStatusCode(); assertTrue((302 == statusCode) || (200 == statusCode)); // Post authentication - if succesfull, we have a 302 and have to redirect if (302 == statusCode) { entity = response.getEntity(); if (entity != null) { EntityUtils.consume(entity); } Header locationHeader = response.getFirstHeader("Location"); String location = locationHeader.getValue(); HttpGet httpGet = new HttpGet(location); response = httpclient.execute(httpGet); } return response; }
From source file:jchat.test.RestTest.java
private String getBody(CloseableHttpResponse response) throws Exception { String body = null;/*from w w w . j a v a2 s .c om*/ try { HttpEntity entity = response.getEntity(); body = EntityUtils.toString(entity); EntityUtils.consume(entity); } finally { response.close(); } return body; }
From source file:org.wso2.carbon.appmgt.mdm.restconnector.utils.RestUtils.java
/** * If not exists generate new access key or return existing one. * * @param remoteServer bean that holds information about remote server * @param generateNewKey whether generate new access key or not * @return generated access key//www.j a v a 2s .com */ public static String getAPIToken(RemoteServer remoteServer, boolean generateNewKey) { if (!generateNewKey) { if (!(AuthHandler.authKey == null || "null".equals(AuthHandler.authKey))) { return AuthHandler.authKey; } } HttpClient httpClient = AppManagerUtil.getHttpClient(remoteServer.getTokenApiURL()); HttpPost postMethod = null; HttpResponse response = null; String responseString = ""; try { List<NameValuePair> nameValuePairs = new ArrayList<>(); nameValuePairs.add( new BasicNameValuePair(Constants.RestConstants.GRANT_TYPE, Constants.RestConstants.PASSWORD)); nameValuePairs .add(new BasicNameValuePair(Constants.RestConstants.USERNAME, remoteServer.getAuthUser())); nameValuePairs .add(new BasicNameValuePair(Constants.RestConstants.PASSWORD, remoteServer.getAuthPass())); URIBuilder uriBuilder = new URIBuilder(remoteServer.getTokenApiURL()); uriBuilder.addParameters(nameValuePairs); postMethod = new HttpPost(uriBuilder.build()); postMethod.setHeader(Constants.RestConstants.AUTHORIZATION, Constants.RestConstants.BASIC + new String(Base64.encodeBase64((remoteServer.getClientKey() + Constants.RestConstants.COLON + remoteServer.getClientSecret()).getBytes()))); postMethod.setHeader(Constants.RestConstants.CONTENT_TYPE, Constants.RestConstants.APPLICATION_FORM_URL_ENCODED); } catch (URISyntaxException e) { String errorMessage = "Cannot construct the Httppost. Url Encoded error."; log.error(errorMessage, e); return null; } try { if (log.isDebugEnabled()) { log.debug("Sending POST request to API Token endpoint. Request path: " + remoteServer.getTokenApiURL()); } response = httpClient.execute(postMethod); int statusCode = response.getStatusLine().getStatusCode(); if (log.isDebugEnabled()) { log.debug("Status code " + statusCode + " received while accessing the API Token endpoint."); } } catch (IOException e) { String errorMessage = "Cannot connect to Token API Endpoint."; log.error(errorMessage, e); return null; } try { HttpEntity entity = response.getEntity(); if (entity != null) { responseString = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } } catch (IOException e) { String errorMessage = "Cannot get response body for auth."; log.error(errorMessage, e); return null; } JSONObject token = (JSONObject) new JSONValue().parse(responseString); AuthHandler.authKey = String.valueOf(token.get(Constants.RestConstants.ACCESS_TOKEN)); return AuthHandler.authKey; }
From source file:org.wso2.identity.integration.test.user.export.UserInfoExportTestCase.java
@Test(alwaysRun = true, groups = "wso2.is", priority = 1, description = "Export user details") public void testExportUserInfo() throws IOException { HttpGet request = new HttpGet(getPiInfoPath()); request.addHeader(HttpHeaders.AUTHORIZATION, getAuthzHeader()); HttpResponse response = client.execute(request); assertEquals(response.getStatusLine().getStatusCode(), 200); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); Object responseObj = JSONValue.parse(rd); EntityUtils.consume(response.getEntity()); Object basicObj = ((JSONObject) responseObj).get("basic"); if (basicObj == null) { Assert.fail();/* ww w.j a v a 2 s . com*/ } else { JSONObject basic = (JSONObject) basicObj; String username = basic.get(USERNAME_CLAIM_URI).toString(); //TODO tenant aware username is coming. is this okay? Assert.assertEquals(username, this.tenantAwareUsername); } }