List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:NginxIT.java
@Test(expected = IllegalArgumentException.class) public void testName() throws Exception { String baseUrl = System.getProperty("cache.base.url"); HttpGet get = new HttpGet(baseUrl); CloseableHttpClient httpClient = HttpClients.createDefault(); try (CloseableHttpResponse response = httpClient.execute(get)) { HttpEntity entity = response.getEntity(); EntityUtils.consume(entity);//from w ww . jav a2s . c o m } }
From source file:org.wso2.mdm.qsg.utils.HTTPInvoker.java
public static HTTPResponse uploadFile(String url, String fileName, String fileContentType) { HttpPost post = null;/*ww w . j a v a 2 s . co m*/ HttpResponse response = null; HTTPResponse httpResponse = new HTTPResponse(); CloseableHttpClient httpclient = null; try { httpclient = (CloseableHttpClient) createHttpClient(); post = new HttpPost(url); File file = new File(fileName); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file, fileContentType); mpEntity.addPart("file", cbFile); post.setEntity(mpEntity); post.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken); //post.setHeader(Constants.Header.CONTENT_TYPE, "multipart/form-data"); post.setHeader("Accept", Constants.ContentType.APPLICATION_JSON); response = httpclient.execute(post); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IOException e) { e.printStackTrace(); } StringBuffer result = new StringBuffer(); String line = ""; try { while ((line = rd.readLine()) != null) { result.append(line); } } catch (IOException e) { e.printStackTrace(); } httpResponse.setResponseCode(response.getStatusLine().getStatusCode()); httpResponse.setResponse(result.toString()); try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } return httpResponse; }
From source file:com.predic8.membrane.core.transport.http.IllegalCharactersInURLTest.java
@Test(expected = IllegalArgumentException.class) public void apacheHttpClient() throws Exception { CloseableHttpClient hc = HttpClientBuilder.create().build(); HttpResponse res = hc.execute(new HttpGet("http://localhost:3027/foo{}")); Assert.assertEquals(200, res.getStatusLine().getStatusCode()); }
From source file:org.fcrepo.kernel.impl.services.ExternalContentServiceImpl.java
/** * Retrieve the content at the URI using the global connection pool. * @param sourceUri the source uri/*from w w w.j a va 2 s .co m*/ * @return the content at the URI using the global connection pool * @throws IOException if IO exception occurred */ @Override public InputStream retrieveExternalContent(final URI sourceUri) throws IOException { final HttpGet httpGet = new HttpGet(sourceUri); final CloseableHttpClient client = getCloseableHttpClient(); final HttpResponse response = client.execute(httpGet); return response.getEntity().getContent(); }
From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.RankerCreationUtil.java
/** * Send a post request to the server to rank answers in the csvAnswerData * /*from ww w .java 2 s . co m*/ * @param client Authorized {@link HttpClient} * @param ranker_url URL of the ranker to hit. Ex.) * https://gateway.watsonplatform.net/retrieve-and-rank/api/v1/ rankers/{ranker_id}/rank * @param csvAnswerData A string with the answer data in csv form * @return JSONObject of the response from the server * @throws ClientProtocolException * @throws IOException * @throws HttpException * @throws JSONException */ public static JSONObject rankAnswers(CloseableHttpClient client, String ranker_url, String csvAnswerData) throws ClientProtocolException, IOException, HttpException, JSONException { // If there is no csv data return an empty array if (csvAnswerData.trim().equals("")) { return new JSONObject("{\"code\":200 , \"answers\" : []}"); } // Create post request to rank answers HttpPost post = new HttpPost(ranker_url); MultipartEntityBuilder postParams = MultipartEntityBuilder.create(); // Fill in post request data postParams.addPart(RetrieveAndRankConstants.ANSWER_DATA, new AnswerFileBody(csvAnswerData)); post.setEntity(postParams.build()); // Send post request and get resulting response HttpResponse response = client.execute(post); String responseString = RankerCreationUtil.getHttpResultString(response); JSONObject responseJSON = null; try { responseJSON = (JSONObject) JSON.parse(responseString); } catch (NullPointerException | JSONException e) { logger.error(e.getMessage()); } if (response.getStatusLine().getStatusCode() != 200) { throw new HttpException(responseString + ":" + post); } return responseJSON; }
From source file:org.epics.archiverappliance.utils.ui.GetUrlContent.java
/** * Post a list of strings to the remove server as a CSV and return the results as a array of JSONObjects * @param url URL// w w w .j a v a2s.co m * @param paramName   * @param params a list of strings * @return JSONArray   * @throws IOException   */ public static JSONArray postStringListAndGetContentAsJSONArray(String url, String paramName, LinkedList<String> params) throws IOException { StringWriter buf = new StringWriter(); buf.append(paramName); buf.append("="); boolean isFirst = true; for (String param : params) { if (isFirst) { isFirst = false; } else { buf.append(","); } buf.append(param); } CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost postMethod = new HttpPost(url); postMethod.addHeader("Content-Type", MimeTypeConstants.APPLICATION_FORM_URLENCODED); postMethod.addHeader("Connection", "close"); // https://www.nuxeo.com/blog/using-httpclient-properly-avoid-closewait-tcp-connections/ StringEntity archiverValues = new StringEntity(buf.toString(), ContentType.APPLICATION_FORM_URLENCODED); 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()) { JSONArray retval = (JSONArray) 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.meli.client.controller.AppController.java
private static void doApiCalls(String query, String countryCode) { List<Item> items = new ArrayList<Item>(); Random r = new Random(); Meli meliOb = new Meli(2072832875526076L, "1VZEFfhbSCy3vDDrh0Dp96NkfgNOWPGq"); try {/*from w w w . jav a 2 s . c om*/ FluentStringsMap params = new FluentStringsMap(); params.add("q", query); String path = countryCode.isEmpty() ? "/sites/MLA/search/" : "/sites/" + countryCode + "/search"; Response response = meliOb.get(path, params); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response.getResponseBody()); JsonNode resultNode = rootNode.findPath("results"); if (resultNode.size() > 0) { JsonNode currNode = null; JsonNode dupNode = null; boolean dupNodeVal = false; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); Item item = null; int randomMins; String checkDupsUrl = null; HttpGet get = null; URIBuilder builder = null; URI uri = null; for (int i = 0; i < resultNode.size(); i++) { currNode = resultNode.get(i); builder = new URIBuilder(); builder.setScheme("http").setHost(apiUrl).setPath("/api/proxy/check") .setParameter("host", "test").setParameter("itemID", currNode.get("id").asText()); uri = builder.build(); get = new HttpGet(uri); get.addHeader("accept", "application/json"); CloseableHttpResponse res = httpClient.execute(get); BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent())); String content = "", line; while ((line = br.readLine()) != null) { content = content + line; } if (!content.isEmpty()) { dupNode = objectMapper.readTree(content); dupNodeVal = Boolean.parseBoolean(dupNode.get("isDuplicate").asText()); if (dupNodeVal && !allowDuplicates) continue; item = new Item(query, currNode.get("id").asText(), "", //currNode.get("host").asText(),?? currNode.get("site_id").asText(), currNode.get("title").asText(), currNode.get("permalink").asText(), currNode.get("category_id").asText(), currNode.get("seller").get("id").asText(), "", //currNode.get("seller").get("name").asText() "", //currNode.get("seller").get("link").asText() "", //currNode.get("seller").get("email").asText() currNode.get("price").asText(), "", //currNode.get("auction_price").asText(), "", //currNode.get("currency_id").asText(), currNode.get("thumbnail").asText()); items.add(item); } randomMins = (int) (Math.random() * (maxWaitTime - minWaitTime)) + minWaitTime; Thread.sleep(randomMins); } if (!items.isEmpty()) { HttpPost post = new HttpPost(apiUrl + "/proxy/add"); StringEntity stringEntity = new StringEntity(objectMapper.writeValueAsString(items)); post.setEntity(stringEntity); post.setHeader("Content-type", "application/json"); CloseableHttpResponse postResponse = httpClient.execute(post); System.out.println("this is the reponse of the final request: " + postResponse.getStatusLine().getStatusCode()); } } } catch (MeliException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.fcrepo.kernel.modeshape.services.ExternalContentServiceImpl.java
/** * Retrieve the content at the URI using the global connection pool. * @param sourceUri the source uri// w w w .jav a2s . c o m * @return the content at the URI using the global connection pool * @throws IOException if IO exception occurred */ @SuppressWarnings("resource") @Override public InputStream retrieveExternalContent(final URI sourceUri) throws IOException { final HttpGet httpGet = new HttpGet(sourceUri); final CloseableHttpClient client = getCloseableHttpClient(); final HttpResponse response = client.execute(httpGet); return response.getEntity().getContent(); }
From source file:com.clonephpscrapper.utility.FetchPageWithProxy.java
public static String fetchPageSourcefromClientGoogleSecond(URI newurl) throws IOException { 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; try {/* ww w .j a v a2s . co 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("400") || responsestatus.contains("401") || responsestatus.contains("402") || responsestatus.contains("403") || responsestatus.contains("407") || responsestatus.contains("404") || responsestatus.contains("405") || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999") || responsestatus.contains("ClientProtocolException") || responsestatus.contains("SocketTimeoutException") || responsestatus == null || "".equals(responsestatus)) { return null; } 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) { return null; } finally { httpclient.close(); } return responsebody; }
From source file:com.gemstone.gemfire.rest.internal.web.controllers.RestAPIsAndInterOpsDUnitTest.java
public static void doGetsUsingRestApis(String restEndpoint) { //HttpHeaders headers = setAcceptAndContentTypeHeaders(); String currentOperation = null; JSONObject jObject;//from w w w. j av a2 s .c om JSONArray jArray; try { //1. Get on key="1" and validate result. { currentOperation = "GET on key 1"; HttpGet get = new HttpGet(restEndpoint + "/People/1"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(get); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer str = new StringBuffer(); while ((line = reader.readLine()) != null) { str.append(line); } jObject = new JSONObject(str.toString()); assertEquals(jObject.get("id"), 101); assertEquals(jObject.get("firstName"), "Mithali"); assertEquals(jObject.get("middleName"), "Dorai"); assertEquals(jObject.get("lastName"), "Raj"); assertEquals(jObject.get("gender"), Gender.FEMALE.name()); } //2. Get on key="16" and validate result. { currentOperation = "GET on key 16"; HttpGet get = new HttpGet(restEndpoint + "/People/16"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(get); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer str = new StringBuffer(); while ((line = reader.readLine()) != null) { str.append(line); } jObject = new JSONObject(str.toString()); assertEquals(jObject.get("id"), 104); assertEquals(jObject.get("firstName"), "Shila"); assertEquals(jObject.get("middleName"), "kumari"); assertEquals(jObject.get("lastName"), "Dixit"); assertEquals(jObject.get("gender"), Gender.FEMALE.name()); } //3. Get all (getAll) entries in Region { HttpGet get = new HttpGet(restEndpoint + "/People"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse result = httpclient.execute(get); assertEquals(result.getStatusLine().getStatusCode(), 200); assertNotNull(result.getEntity()); HttpEntity entity = result.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line); } result.close(); try { jObject = new JSONObject(sb.toString()); jArray = jObject.getJSONArray("People"); assertEquals(jArray.length(), 16); } catch (JSONException e) { fail(" Rest Request ::" + currentOperation + " :: should not have thrown JSONException "); } } //4. GetAll?limit=10 (10 entries) and verify results { HttpGet get = new HttpGet(restEndpoint + "/People?limit=10"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(get); assertEquals(response.getStatusLine().getStatusCode(), 200); assertNotNull(response.getEntity()); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer str = new StringBuffer(); while ((line = reader.readLine()) != null) { str.append(line); } try { jObject = new JSONObject(str.toString()); jArray = jObject.getJSONArray("People"); assertEquals(jArray.length(), 10); } catch (JSONException e) { fail(" Rest Request ::" + currentOperation + " :: should not have thrown JSONException "); } } //5. Get keys - List all keys in region { HttpGet get = new HttpGet(restEndpoint + "/People/keys"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(get); assertEquals(response.getStatusLine().getStatusCode(), 200); assertNotNull(response.getEntity()); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer str = new StringBuffer(); while ((line = reader.readLine()) != null) { str.append(line); } try { jObject = new JSONObject(str.toString()); jArray = jObject.getJSONArray("keys"); assertEquals(jArray.length(), 16); } catch (JSONException e) { fail(" Rest Request ::" + currentOperation + " :: should not have thrown JSONException "); } } //6. Get data for specific keys { HttpGet get = new HttpGet(restEndpoint + "/People/1,3,5,7,9,11"); get.addHeader("Content-Type", "application/json"); get.addHeader("Accept", "application/json"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(get); assertEquals(response.getStatusLine().getStatusCode(), 200); assertNotNull(response.getEntity()); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; StringBuffer str = new StringBuffer(); while ((line = reader.readLine()) != null) { str.append(line); } try { jObject = new JSONObject(str.toString()); jArray = jObject.getJSONArray("People"); assertEquals(jArray.length(), 6); } catch (JSONException e) { fail(" Rest Request ::" + currentOperation + " :: should not have thrown JSONException "); } } } catch (Exception e) { throw new RuntimeException("unexpected exception", e); } }