List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:com.intel.cosbench.client.amplistor.AmpliClient.java
public String StoreObject(String sourceFilename, String ampliNamespace, String ampliFilename) throws IOException, HttpException, AmpliException { File file = new File(sourceFilename); HttpPut method = null;/*from w w w .j a v a 2 s. c o m*/ HttpResponse response = null; try { String storageUrl = "http://" + this.host + ":" + this.port + nsRoot; method = HttpClientUtil.makeHttpPut(storageUrl + "/" + HttpClientUtil.encodeURL(ampliNamespace) + "/" + HttpClientUtil.encodeURL(ampliFilename)); method.setEntity(new FileEntity(file, "application/octet-stream")); response = client.execute(method); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return response.getFirstHeader("ETag").getValue(); } if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) { return response.getFirstHeader("ETag").getValue(); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_PRECONDITION_FAILED) { throw new AmpliException("Etag missmatch", response.getAllHeaders(), response.getStatusLine()); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_LENGTH_REQUIRED) { throw new AmpliException("Length miss-match", response.getAllHeaders(), response.getStatusLine()); } else { throw new AmpliException("Unexpected Server Response: " + response.getStatusLine(), response.getAllHeaders(), response.getStatusLine()); } } finally { if (response != null) EntityUtils.consume(response.getEntity()); } }
From source file:com.sumologic.logback.SumoLogicAppender.java
private void sendToSumo(String data) { HttpPost post = null;/* w ww . ja va 2 s . c o m*/ try { post = new HttpPost(url); post.setEntity(new StringEntity(data, HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8)); HttpResponse response = httpClient.execute(post); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { log.warn("Received HTTP error from Sumo Service: {}", statusCode); } //need to consume the body if you want to re-use the connection. EntityUtils.consume(response.getEntity()); } catch (IOException e) { log.warn("Could not send log to Sumo Logic", e); try { post.abort(); } catch (Exception ignore) { } } }
From source file:org.wuspba.ctams.ws.ITPersonController.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.getPeople().size(), 5); for (Person p : doc.getPeople()) { if (p.getLastName().equals(TestFixture.INSTANCE.andy.getLastName())) { testEquality(p, TestFixture.INSTANCE.andy); } else if (p.getLastName().equals(TestFixture.INSTANCE.bob.getLastName())) { testEquality(p, TestFixture.INSTANCE.bob); } else if (p.getLastName().equals(TestFixture.INSTANCE.elaine.getLastName())) { testEquality(p, TestFixture.INSTANCE.elaine); } else if (p.getLastName().equals(TestFixture.INSTANCE.eoin.getLastName())) { testEquality(p, TestFixture.INSTANCE.eoin); } else if (p.getLastName().equals(TestFixture.INSTANCE.jamie.getLastName())) { testEquality(p, TestFixture.INSTANCE.jamie); } else { fail();// ww w. ja v a 2s. co m } } EntityUtils.consume(entity); } }
From source file:org.fcrepo.integration.connector.file.FileConnectorIT.java
/** * I should be able to link to content on a federated filesystem. * * @throws IOException thrown during this function **///from w ww . j av a 2s . c o m @Test public void testFederatedDatastream() throws IOException { final String federationAddress = serverAddress + "files/FileSystem1/ds1"; final String linkingAddress; try (final CloseableHttpResponse response = client.execute(new HttpPost(serverAddress))) { EntityUtils.consume(response.getEntity()); linkingAddress = response.getFirstHeader("Location").getValue(); } // link from the object to the content of the file on the federated filesystem final HttpPatch patch = new HttpPatch(linkingAddress); patch.addHeader("Content-Type", "application/sparql-update"); patch.setEntity(new ByteArrayEntity(("INSERT DATA { <> <http://some-vocabulary#hasExternalContent> " + "<" + federationAddress + "> . }").getBytes())); assertEquals("Couldn't link to external datastream!", NO_CONTENT.getStatusCode(), getStatus(patch)); }
From source file:com.photon.phresco.framework.impl.CIManagerImplTest.java
public void testRun() throws Exception { System.out.println("Test 1234 "); HttpClient client = new DefaultHttpClient(); String uri = "http://172.16.22.180:3579/ci/j_acegi_security_check"; HttpPost post = new HttpPost(uri); HttpContext httpContext = new BasicHttpContext(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("j_username", "admin")); nameValuePairs.add(new BasicNameValuePair("j_password", "admin")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post, httpContext); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); EntityUtils.consume(response.getEntity()); HttpGet request = new HttpGet("http://172.16.22.180:3579/ci"); HttpResponse response1 = client.execute(request); int responseCode1 = response1.getStatusLine().getStatusCode(); System.out.println("Response Code : " + responseCode1); printResponse(response1);/*from ww w . ja va2s . c o m*/ }
From source file:justdailscrapper.vik.utility.FetchPageWithoutProxy.java
public static String fetchPageSourcefromClientGoogle(URI newurl, List<ProxyImport> proxyList) throws IOException, InterruptedException { Random r = new Random(); ProxyImport obj = proxyList.get(r.nextInt(proxyList.size())); String ip = obj.proxyIP;/*from www . ja v a 2s .co m*/ int portno = Integer.parseInt(obj.proxyPort); String username = ""; String password = ""; if (obj.proxyLen > 2) { username = obj.proxyUserName; password = obj.proxyPassword; } // int portNo = generateRandomPort(); CredentialsProvider credsprovider = new BasicCredentialsProvider(); credsprovider.setCredentials(new AuthScope(ip, portno), new UsernamePasswordCredentials(username, password)); HttpHost proxy = new HttpHost(ip, 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.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0") .setDefaultRequestConfig(requestConfig).setProxy(proxy).build(); String responsebody = ""; String responsestatus = null; int count = 0; try { HttpGet httpget = new HttpGet(newurl); httpget.addHeader("Accept", "*/*"); httpget.addHeader("Accept-Encoding", "gzip, deflate, br"); httpget.addHeader("Accept-Language", "en-US,en;q=0.5"); httpget.addHeader("Host", "www.justdial.com"); httpget.addHeader("Referer", "https://www.justdial.com/Bangalore/Yamaha-Two-Wheeler-Dealers"); httpget.addHeader("Connection", "keep-alive"); httpget.addHeader("X-Requested-With", "XMLHttpReques"); 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, proxyList); 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, proxyList); 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.flicklib.service.HttpClientSourceLoader.java
@Override public Source post(String url, Map<String, String> parameters, Map<String, String> headers) throws IOException { HttpResponse response = null;/*from ww w . j a v a 2 s.c o m*/ try { LOGGER.info("Loading " + url); HttpPost httpMethod = new HttpPost(url); if (parameters != null && !parameters.isEmpty()) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Entry<String, String> entry : parameters.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8"); httpMethod.setEntity(form); } if (headers != null && !headers.isEmpty()) { for (Entry<String, String> entry : headers.entrySet()) { httpMethod.addHeader(entry.getKey(), entry.getValue()); } } HttpContext ctx = new BasicHttpContext(); response = client.execute(httpMethod, ctx); return buildSource(url, response, httpMethod, ctx); } finally { EntityUtils.consume(response.getEntity()); } }
From source file:org.mahasen.client.Upload.java
/** * @param uploadFile/*w ww .j a va 2s . c o m*/ * @param tags * @param folderStructure * @param addedProperties * @throws IOException */ public void upload(File uploadFile, String tags, String folderStructure, List<NameValuePair> addedProperties) throws IOException, MahasenClientException, URISyntaxException { httpclient = new DefaultHttpClient(); if (addedProperties != null) { this.customProperties = addedProperties; } try { System.out.println(" Is Logged : " + clientLoginData.isLoggedIn()); if (clientLoginData.isLoggedIn() == true) { httpclient = WebClientSSLWrapper.wrapClient(httpclient); File file = uploadFile; if (file.exists()) { if (!folderStructure.equals("")) { customProperties.add(new BasicNameValuePair("folderStructure", folderStructure)); } customProperties.add(new BasicNameValuePair("fileName", file.getName())); customProperties.add(new BasicNameValuePair("tags", tags)); URI uri = URIUtils.createURI("https", clientLoginData.getHostNameAndPort(), -1, "/mahasen/upload_ajaxprocessor.jsp", URLEncodedUtils.format(customProperties, "UTF-8"), null); HttpPost httppost = new HttpPost(uri); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); httppost.setHeader("testHeader", "testHeadervalue"); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(resEntity); if (response.getStatusLine().getStatusCode() == 900) { throw new MahasenClientException(String.valueOf(response.getStatusLine())); } } } else { System.out.println("User has to be logged in to perform this function"); } } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:httpServerClient.app.QuickStart.java
public static void getAllMessages(String[] args) throws Exception { // arguments to run Quick start: // args[0] GET // args[1] IPAddress of server // args[2] port No. CloseableHttpClient httpclient = HttpClients.createDefault(); JacksonObjectMapperToList myList = new JacksonObjectMapperToList(); try {/*w w w. jav a 2 s. c o m*/ HttpGet httpGet = new HttpGet("http://" + args[1] + ":" + args[2] + "/getAllMessages"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response // object // to allow the response content to be streamed directly from the // network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally // clause. // Please note that if response content is not fully consumed the // underlying // connection cannot be safely re-used and will be shut down and // discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); String result = EntityUtils.toString(entity1); /* * Vypisanie odpovede do konzoly a discard dat zo serveru. */ //myList.jsonToList(result); myList.jacksonToList(result); myList.PrintList(); EntityUtils.consume(entity1); } finally { response1.close(); } } finally { httpclient.close(); } }
From source file:org.picketbox.test.authentication.http.jetty.DelegatingSecurityFilterHTTPDigestUnitTestCase.java
@Test public void testDigestAuth() throws Exception { URL url = new URL(urlStr); DefaultHttpClient httpclient = null; try {/*from w w w.j a va 2s . c o m*/ String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); assertEquals(401, response.getStatusLine().getStatusCode()); Header[] headers = response.getHeaders(PicketBoxConstants.HTTP_WWW_AUTHENTICATE); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); Header header = headers[0]; String value = header.getValue(); value = value.substring(7).trim(); String[] tokens = HTTPDigestUtil.quoteTokenize(value); DigestHolder digestHolder = HTTPDigestUtil.digest(tokens); DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("algorithm", "MD5"); digestAuth.overrideParamter("realm", digestHolder.getRealm()); digestAuth.overrideParamter("nonce", digestHolder.getNonce()); digestAuth.overrideParamter("qop", "auth"); digestAuth.overrideParamter("nc", "0001"); digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce()); digestAuth.overrideParamter("opaque", digestHolder.getOpaque()); httpget = new HttpGet(url.toExternalForm()); Header auth = digestAuth.authenticate(new UsernamePasswordCredentials(user, pass), httpget); System.out.println(auth.getName()); System.out.println(auth.getValue()); httpget.setHeader(auth); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(200, statusLine.getStatusCode()); EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }