下面列出了org.apache.http.client.methods.HttpHead#releaseConnection() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
/**
* 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();
}
}
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();
}
}
/**
* 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();
}
}
@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();
}