Java Code Examples for org.apache.http.client.methods.HttpHead#releaseConnection()

The following examples show how to use org.apache.http.client.methods.HttpHead#releaseConnection() . 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: HeadIT.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testHeadToGetSwitch() throws Exception {
    HttpHead head = new HttpHead(getHttpURl("/hello/html"));
    // When checking the content length, we must disable the compression:
    head.setHeader(HeaderNames.ACCEPT_ENCODING, "identity");
    HttpResponse<String> response;
    try {
        org.apache.http.HttpResponse resp = ClientFactory.getHttpClient().execute(head);
        response = new HttpResponse<>(resp, String.class);
    } finally {
        head.releaseConnection();
    }

    assertThat(response.code()).isEqualTo(OK);
    assertThat(response.contentType()).isEqualTo(MimeTypes.HTML);
    System.out.println(response.headers());
    assertThat(Integer.valueOf(response.header(CONTENT_LENGTH))).isEqualTo(20);
}
 
Example 2
Source File: MgrSyncUtils.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Send a HEAD request to a given URL to verify accessibility with given credentials.
 *
 * @param url the URL to verify
 * @param username username for authentication (pass null for unauthenticated requests)
 * @param password password for authentication (pass null for unauthenticated requests)
 * @param ignoreNoProxy set true to ignore the "no_proxy" setting
 * @return the response code of the request
 * @throws IOException in case of an error
 */
private static HttpResponse sendHeadRequest(String url, String username,
        String password, boolean ignoreNoProxy) throws IOException {
    HttpClientAdapter httpClient = new HttpClientAdapter();
    HttpHead headRequest = new HttpHead(url);
    try {
        return httpClient.executeRequest(
                headRequest, username, password, ignoreNoProxy);
    }
    finally {
        headRequest.releaseConnection();
    }
}
 
Example 3
Source File: HttpRequestHandler.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
public int getHttpStatus (String url) throws IOException {
    String encodedUrl = getEncodedUrl(url);
    CloseableHttpClient httpClient = getHttpClient(encodedUrl);
    HttpHead head = new HttpHead(encodedUrl);
    try {
        LOGGER.debug("executing head request to retrieve page status on " + head.getURI());
        HttpResponse response = httpClient.execute(head);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("received " + response.getStatusLine().getStatusCode() + " from head request");
            for (Header h : head.getAllHeaders()) {
                LOGGER.debug("header : " + h.getName() + " " + h.getValue());
            }
        }
        
        return response.getStatusLine().getStatusCode();
    } catch (UnknownHostException uhe) {
        LOGGER.warn("UnknownHostException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IllegalArgumentException iae) {
        LOGGER.warn("IllegalArgumentException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IOException ioe) {
        LOGGER.warn("IOException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        head.releaseConnection();
        httpClient.close();
    }
}
 
Example 4
Source File: Client.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Send a HEAD request
 * @param cluster the cluster definition
 * @param path the path or URI
 * @param headers the HTTP headers to include in the request
 * @return a Response object with response detail
 * @throws IOException
 */
public Response head(Cluster cluster, String path, Header[] headers)
    throws IOException {
  HttpHead method = new HttpHead(path);
  try {
    HttpResponse resp = execute(cluster, method, null, path);
    return new Response(resp.getStatusLine().getStatusCode(), resp.getAllHeaders(), null);
  } finally {
    method.releaseConnection();
  }
}
 
Example 5
Source File: HeadIT.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeadToGetSwitchOnMissingPage() throws Exception {
    HttpHead head = new HttpHead(getHttpURl("/hello/missing"));

    HttpResponse<String> response;
    try {
        org.apache.http.HttpResponse resp = ClientFactory.getHttpClient().execute(head);
        response = new HttpResponse<>(resp, String.class);
    } finally {
        head.releaseConnection();
    }

    assertThat(response.code()).isEqualTo(NOT_FOUND);
    assertThat(response.body()).isNull();
}