List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:com.codedx.burp.ExportActionListener.java
private HttpResponse sendData(File data, String urlStr) throws IOException { CloseableHttpClient client = burpExtender.getHttpClient(); if (client == null) return null; HttpPost post = new HttpPost(urlStr); post.setHeader("API-Key", burpExtender.getApiKey()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("file", new FileBody(data)); HttpEntity entity = builder.build(); post.setEntity(entity);//from w w w. j a v a 2 s .co m HttpResponse response = client.execute(post); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { EntityUtils.consume(resEntity); } client.close(); return response; }
From source file:com.floragunn.searchguard.test.helper.rest.RestHelper.java
public String executeSimpleRequest(final String request) throws Exception { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try {/* www. j a v a 2 s . c o m*/ httpClient = getHTTPClient(); response = httpClient.execute(new HttpGet(getHttpServerUri() + "/" + request)); if (response.getStatusLine().getStatusCode() >= 300) { throw new Exception("Statuscode " + response.getStatusLine().getStatusCode()); } return IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); } finally { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } }
From source file:javaeetutorial.web.websocketbot.BotBean.java
public String get(String msg, String uri) { msg = msg.toLowerCase().replaceAll("\\?", ""); CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w .ja v a 2s . c o m*/ HttpGet httpget = new HttpGet(uri + msg); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println(response.getStatusLine()); if (entity != null) { String jsonresult = EntityUtils.toString(entity); System.out.println("Response content:" + jsonresult); return jsonresult; } } catch (Exception e) { // TODO: handle exception } } catch (Exception e) { // TODO: handle exception } finally { try { httpclient.close(); } catch (Exception e2) { // TODO: handle exception } } return null; }
From source file:com.srotya.sidewinder.cluster.storage.ClusteredMemStorageEngine.java
@Override public void dropDatabase(String dbName) throws Exception { List<String> proxies = new ArrayList<>(); dbName = decodeDbAndProxyNames(proxies, dbName); if (proxies.size() > 0) { local.dropDatabase(dbName);//w w w . ja v a2 s .c om } else { local.dropDatabase(dbName); for (Entry<Integer, WorkerEntry> entry : columbus.getWorkerMap().entrySet()) { if (entry.getKey() != columbus.getSelfWorkerId()) { String newDbName = encodeDbAndProxyName(dbName, String.valueOf(columbus.getSelfWorkerId())); // http call CloseableHttpClient client = Utils.getClient( "http://" + entry.getValue().getWorkerAddress().getHostAddress() + ":8080/", 5000, 5000); HttpDelete post = new HttpDelete("http://" + entry.getValue().getWorkerAddress().getHostAddress() + ":8080/database/" + newDbName); CloseableHttpResponse result = client.execute(post); result.close(); client.close(); } } } }
From source file:org.knoxcraft.http.client.ClientMultipartFormPost.java
public static void upload(String url, String playerName, File jsonfile, File sourcefile) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// ww w .j av a 2 s . com HttpPost httppost = new HttpPost(url); FileBody json = new FileBody(jsonfile); FileBody source = new FileBody(sourcefile); String jsontext = readFromFile(jsonfile); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("playerName", new StringBody(playerName, ContentType.TEXT_PLAIN)) .addPart("jsonfile", json).addPart("sourcefile", source) .addPart("language", new StringBody("java", ContentType.TEXT_PLAIN)) .addPart("jsontext", new StringBody(jsontext, ContentType.TEXT_PLAIN)).addPart("sourcetext", new StringBody("public class Foo {\n int x=5\n}", ContentType.TEXT_PLAIN)) .build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { //System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { //System.out.println("Response content length: " + resEntity.getContentLength()); Scanner sc = new Scanner(resEntity.getContent()); while (sc.hasNext()) { System.out.println(sc.nextLine()); } sc.close(); EntityUtils.consume(resEntity); } } finally { response.close(); } } finally { httpclient.close(); } }
From source file:org.cloudsimulator.utility.RestAPI.java
public static ResponseMessageString sendString(final String requestMethod, final String restAPIURI, final String username, final String password, final String stringToSend, final String typeOfString, final String charset) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; ResponseMessageString responseMessageString = null; if ("PUT".equals(requestMethod)) { httpResponse = putRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfString, new ByteArrayEntity(stringToSend.getBytes(charset))); }//from w w w . ja v a2 s. c o m if ("POST".equals(requestMethod)) { httpResponse = postRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfString, new ByteArrayEntity(stringToSend.getBytes(charset))); } if (httpResponse != null) { if (httpResponse.getStatusLine() != null) { if (httpResponse.getEntity() != null) { responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), IOUtils.toString(httpResponse.getEntity().getContent(), charset)); } else { responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), null); } } else { if (httpResponse.getEntity() != null) { responseMessageString = new ResponseMessageString(null, null, IOUtils.toString(httpResponse.getEntity().getContent(), charset)); } } httpResponse.close(); } httpClient.close(); return responseMessageString; }
From source file:org.fao.geonet.api.records.editing.InspireValidatorUtils.java
/** * Submit file./*from www.j a v a 2s .c o m*/ * * @param record the record * @return the string * @throws IOException Signals that an I/O exception has occurred. * @throws JSONException the JSON exception */ public static String submitFile(String serviceEndpoint, InputStream record, String testTitle) throws IOException, JSONException { CloseableHttpClient client = HttpClients.createDefault(); try { if (InspireValidatorUtils.checkServiceStatus(serviceEndpoint, client)) { // Get the tests to execute List<String> tests = InspireValidatorUtils.getTests(serviceEndpoint, client); // Upload file to test String testFileId = InspireValidatorUtils.uploadMetadataFile(serviceEndpoint, record, client); if (testFileId == null) { Log.error(Log.SERVICE, "File not valid.", new Exception()); return null; } if (tests == null || tests.size() == 0) { Log.error(Log.SERVICE, "Default test sequence not supported. Check org.fao.geonet.api.records.editing.InspireValidatorUtils.TESTS_TO_RUN.", new Exception()); return null; } // Return test id from Inspire service return InspireValidatorUtils.testRun(serviceEndpoint, testFileId, tests, testTitle, client); } else { ServiceNotFoundEx ex = new ServiceNotFoundEx(serviceEndpoint); Log.error(Log.SERVICE, "Service unavailable.", ex); throw ex; } } finally { client.close(); } }
From source file:com.fpmislata.banco.business.service.impl.BancoCentralServiceImpl.java
@Override public CredencialesBancarias getURLByCCC(String ccc) throws BusinessException { CloseableHttpClient httpClient = HttpClients.createDefault(); try {/*from ww w . j a va 2 s .c om*/ HttpGet httpGet = new HttpGet(""); // System.out.println("GET Response Status:: " // + httpResponse.getStatusLine().getStatusCode()); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String response = httpClient.execute(httpGet, responseHandler); return jsonTransformer.fromJSON(response, CredencialesBancarias.class); } catch (IOException ex) { Logger.getLogger(BancoCentralServiceImpl.class.getName()).log(Level.SEVERE, null, ex); // throw new BusinessException("Error por cuestiones ajenas", "BancoCentral"); return this.getURLByCCCLocal(ccc); } finally { try { httpClient.close(); } catch (IOException ex) { Logger.getLogger(BancoCentralServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.alibaba.jstorm.daemon.nimbus.metric.uploader.AlimonitorClient.java
private boolean httpGet(StringBuilder postAddr) { boolean ret = false; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse response = null; try {/*from ww w . j a va2s . c om*/ HttpGet request = new HttpGet(postAddr.toString()); response = httpClient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { LOG.info(EntityUtils.toString(entity)); } EntityUtils.consume(entity); ret = true; } catch (Exception e) { LOG.error("Exception when sending http request to ali monitor", e); } finally { try { if (response != null) response.close(); httpClient.close(); } catch (Exception e) { LOG.error("Exception when closing httpclient", e); } } return ret; }
From source file:org.jboss.additional.testsuite.jdkall.present.web.servlet.headers.CookieHeaderServletTestCase.java
@Test @Ignore//from w ww .j av a 2 s. co m @OperateOnDeployment(DEPLOYMENT2) public void cookieHeaderCommaSeparatorTest(@ArquillianResource URL url2) throws Exception { URL testURL = new URL(url2.toString() + "cookieHeaderServlet2"); CloseableHttpClient httpClient = HttpClients.createDefault(); final HttpGet request = new HttpGet(testURL.toString()); CloseableHttpResponse response = null; response = httpClient.execute(request); response = httpClient.execute(request); Assert.assertTrue("The cookie length should be 2.", response.getFirstHeader("cookies.length").getValue().compareTo("2") == 0); Assert.assertTrue("The cookie value1 should be example_cookie.", response.getFirstHeader("cookies.value1").getValue().compareTo("example_cookie") == 0); Assert.assertTrue("The cookie value2 should be example2_cookie.", response.getFirstHeader("cookies.value2").getValue().compareTo("example2_cookie") == 0); IOUtils.closeQuietly(response); httpClient.close(); }