下面列出了org.apache.http.client.methods.HttpDelete#releaseConnection() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void remove() throws IOException, UnexpectedResponseException {
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
this.baseUri.toString()
);
final HttpDelete delete = new HttpDelete(
uri.build()
);
try {
this.client.execute(
delete,
new MatchStatus(delete.getURI(), HttpStatus.SC_NO_CONTENT)
);
} finally {
delete.releaseConnection();
}
}
@Override
public void remove(final boolean force)
throws IOException, UnexpectedResponseException {
final UncheckedUriBuilder uri = new UncheckedUriBuilder(
this.baseUri.toString()
);
uri.addParameter("force", String.valueOf(force));
final HttpDelete delete = new HttpDelete(
uri.build()
);
try {
this.client.execute(
delete,
new MatchStatus(delete.getURI(), HttpStatus.SC_OK)
);
} finally {
delete.releaseConnection();
}
}
public HttpResponse doDelete(String resourcePath) throws RestClientException {
HttpDelete delete = new HttpDelete(resourcePath);
setAuthHeader(delete);
try {
return httpClient.execute(delete);
} catch (IOException e) {
String errorMsg = "Error while executing DELETE statement";
log.error(errorMsg, e);
throw new RestClientException(errorMsg, e);
} finally {
delete.releaseConnection();
}
}
@Test
public void testDeleteBook() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/bookstore/books/123";
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpDelete delete = new HttpDelete(endpointAddress);
try {
CloseableHttpResponse response = client.execute(delete);
assertEquals(200, response.getStatusLine().getStatusCode());
} finally {
// Release current connection to the connection pool once you are done
delete.releaseConnection();
}
}
@Test
public void testDeleteBookByQuery() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/bookstore/books/id?value=123";
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpDelete delete = new HttpDelete(endpointAddress);
try {
CloseableHttpResponse response = client.execute(delete);
assertEquals(200, response.getStatusLine().getStatusCode());
} finally {
// Release current connection to the connection pool once you are done
delete.releaseConnection();
}
}
protected void executeDeleteObject(String uri) throws BrocadeVcsApiException {
if (_host == null || _host.isEmpty() || _adminuser == null || _adminuser.isEmpty() || _adminpass == null || _adminpass.isEmpty()) {
throw new BrocadeVcsApiException("Hostname/credentials are null or empty");
}
final HttpDelete dm = (HttpDelete)createMethod("delete", uri);
dm.setHeader("Accept", "application/vnd.configuration.resource+xml");
final HttpResponse response = executeMethod(dm);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
String errorMessage;
try {
errorMessage = responseToErrorMessage(response);
} catch (final IOException e) {
s_logger.error("Failed to delete object : " + e.getMessage());
throw new BrocadeVcsApiException("Failed to delete object : " + e.getMessage());
}
dm.releaseConnection();
s_logger.error("Failed to delete object : " + errorMessage);
throw new BrocadeVcsApiException("Failed to delete object : " + errorMessage);
}
dm.releaseConnection();
}
public static String delete(String url) throws IOException {
HttpDelete httpDelete = deleteForm(url);
HttpResponse response;
String result = null;
try {
response = httpclient.execute(httpDelete);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} finally {
httpDelete.releaseConnection();
}
return result;
}
/**
* Send a DELETE request
* @param cluster the cluster definition
* @param path the path or URI
* @return a Response object with response detail
* @throws IOException for error
*/
public Response delete(Cluster cluster, String path) throws IOException {
HttpDelete method = new HttpDelete(path);
try {
HttpResponse resp = execute(cluster, method, null, path);
Header[] headers = resp.getAllHeaders();
byte[] content = getResponseBody(resp);
return new Response(resp.getStatusLine().getStatusCode(), headers, content);
} finally {
method.releaseConnection();
}
}
/**
* Send a DELETE request
* @param cluster the cluster definition
* @param path the path or URI
* @return a Response object with response detail
* @throws IOException for error
*/
public Response delete(Cluster cluster, String path, Header extraHdr) throws IOException {
HttpDelete method = new HttpDelete(path);
try {
Header[] headers = { extraHdr };
HttpResponse resp = execute(cluster, method, headers, path);
headers = resp.getAllHeaders();
byte[] content = getResponseBody(resp);
return new Response(resp.getStatusLine().getStatusCode(), headers, content);
} finally {
method.releaseConnection();
}
}