下面列出了org.apache.http.client.methods.HttpPut#releaseConnection() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void announceWipeCache(String entity, String event, String cacheKey) throws IOException {
String url = baseUrl + "/cache/announce/" + entity + "/" + cacheKey + "/" + event;
HttpPut request = new HttpPut(url);
try {
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
String msg = EntityUtils.toString(response.getEntity());
throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with announce cache wipe url " + url + "\n" + msg);
}
} catch (Exception ex) {
throw new IOException(ex);
} finally {
request.releaseConnection();
}
}
@Test
public void testUpdateBook() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/bookstore/books";
File input = new File(getClass().getResource("resources/update_book.txt").toURI());
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPut put = new HttpPut(endpointAddress);
put.setEntity(new FileEntity(input, ContentType.TEXT_XML));
try {
CloseableHttpResponse response = client.execute(put);
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
stripXmlInstructionIfNeeded(EntityUtils.toString(response.getEntity())));
} finally {
// Release current connection to the connection pool once you are
// done
put.releaseConnection();
}
}
@Test
public void testUpdateBookWithDom() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithdom";
File input = new File(getClass().getResource("resources/update_book.txt").toURI());
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPut put = new HttpPut(endpointAddress);
put.setEntity(new FileEntity(input, ContentType.TEXT_XML));
try {
CloseableHttpResponse response = client.execute(put);
assertEquals(200, response.getStatusLine().getStatusCode());
String resp = EntityUtils.toString(response.getEntity());
InputStream expected = getClass().getResourceAsStream("resources/update_book.txt");
String s = getStringFromInputStream(expected);
//System.out.println(resp);
//System.out.println(s);
assertTrue(resp.indexOf(s) >= 0);
} finally {
// Release current connection to the connection pool once you are
// done
put.releaseConnection();
}
}
@Test
public void testUpdateBookWithJSON() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithjson";
File input = new File(getClass().getResource("resources/update_book_json.txt").toURI());
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPut put = new HttpPut(endpointAddress);
put.setEntity(new FileEntity(input, ContentType.APPLICATION_JSON));
try {
CloseableHttpResponse response = client.execute(put);
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt");
assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)),
stripXmlInstructionIfNeeded(EntityUtils.toString(response.getEntity())));
} finally {
// Release current connection to the connection pool once you are
// done
put.releaseConnection();
}
}
@Test
public void testUpdateBookFailed() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/bookstore/books";
File input = new File(getClass().getResource("resources/update_book_not_exist.txt").toURI());
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPut put = new HttpPut(endpointAddress);
put.setEntity(new FileEntity(input, ContentType.TEXT_XML));
try {
CloseableHttpResponse response = client.execute(put);
assertEquals(304, response.getStatusLine().getStatusCode());
} finally {
// Release current connection to the connection pool once you are done
put.releaseConnection();
}
}
public static String put(String url, JSONObject data) throws IOException {
HttpPut httpPut = putForm(url, data);
HttpResponse response;
String result = null;
try {
response = httpclient.execute(httpPut);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} finally {
httpPut.releaseConnection();
}
return result;
}
/**
* Send a PUT request
* @param cluster the cluster definition
* @param path the path or URI
* @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
* supplied
* @param content the content bytes
* @return a Response object with response detail
* @throws IOException
*/
public Response put(Cluster cluster, String path, Header[] headers,
byte[] content) throws IOException {
HttpPut method = new HttpPut(path);
try {
method.setEntity(new InputStreamEntity(new ByteArrayInputStream(content), content.length));
HttpResponse resp = execute(cluster, method, headers, path);
headers = resp.getAllHeaders();
content = getResponseBody(resp);
return new Response(resp.getStatusLine().getStatusCode(), headers, content);
} finally {
method.releaseConnection();
}
}
@Override
public void append(String content) throws IOException {
HttpPut putMethod = new HttpPut(consoleUri);
try {
LOGGER.debug("Appending console to URL -> {}", consoleUri);
StringEntity entity = new StringEntity(content, charset);
putMethod.setEntity(entity);
HttpService.setSizeHeader(putMethod, entity.getContentLength());
try (CloseableHttpResponse response = httpService.execute(putMethod)) {
LOGGER.debug("Got {}", response.getStatusLine().getStatusCode());
}
} finally {
putMethod.releaseConnection();
}
}