本文整理了Java中org.apache.http.client.methods.CloseableHttpResponse.close()
方法的一些代码示例,展示了CloseableHttpResponse.close()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CloseableHttpResponse.close()
方法的具体详情如下:
包路径:org.apache.http.client.methods.CloseableHttpResponse
类名称:CloseableHttpResponse
方法名:close
暂无
代码示例来源:origin: stackoverflow.com
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
HttpGet httpGet = new HttpGet("https://some-server");
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}
finally {
response.close();
}
代码示例来源:origin: alibaba/jstorm
private boolean httpGet(StringBuilder postAddr) {
boolean ret = false;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
HttpGet request = new HttpGet(postAddr.toString());
response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
LOG.info(EntityUtils.toString(entity));
}
EntityUtils.consume(entity);
ret = true;
} catch (Exception e) {
LOG.error("Exception when sending http request to ali monitor", e);
} finally {
try {
if (response != null)
response.close();
httpClient.close();
} catch (Exception e) {
LOG.error("Exception when closing httpclient", e);
}
}
return ret;
}
代码示例来源:origin: liyiorg/weixin-popular
private static BufferedImage getImage(CloseableHttpResponse httpResponse) {
try {
int status = httpResponse.getStatusLine().getStatusCode();
if (status == 200) {
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
return ImageIO.read(new ByteArrayInputStream(bytes));
}
} catch (IOException e) {
logger.error("", e);
} finally {
try {
httpResponse.close();
} catch (IOException e) {
logger.error("", e);
}
}
return null;
}
代码示例来源:origin: brianfrankcooper/YCSB
private int httpDelete(String endpoint) throws IOException {
requestTimedout.setIsSatisfied(false);
Thread timer = new Thread(new Timer(execTimeout, requestTimedout));
timer.start();
int responseCode = 200;
HttpDelete request = new HttpDelete(endpoint);
for (int i = 0; i < headers.length; i = i + 2) {
request.setHeader(headers[i], headers[i + 1]);
}
CloseableHttpResponse response = client.execute(request);
responseCode = response.getStatusLine().getStatusCode();
response.close();
client.close();
return responseCode;
}
代码示例来源:origin: apache/incubator-gobblin
/**
* Deletes a project. Currently no response message will be returned after finishing
* the delete operation. Thus success status is always expected.
*
* @param projectName project name
*
* @return A status object indicating if AJAX request is successful.
*/
public AzkabanClientStatus deleteProject(String projectName) {
try {
refreshSession();
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair(AzkabanClientParams.DELETE, "true"));
nvps.add(new BasicNameValuePair(AzkabanClientParams.SESSION_ID, this.sessionId));
nvps.add(new BasicNameValuePair(AzkabanClientParams.PROJECT, projectName));
Header contentType = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
Header requestType = new BasicHeader("X-Requested-With", "XMLHttpRequest");
HttpGet httpGet = new HttpGet(url + "/manager?" + URLEncodedUtils.format(nvps, "UTF-8"));
httpGet.setHeaders(new Header[]{contentType, requestType});
CloseableHttpResponse response = this.httpClient.execute(httpGet);
response.close();
return new AzkabanClientStatus.SUCCESS();
} catch (Exception e) {
return new AzkabanClientStatus.FAIL("Azkaban client cannot delete project = "
+ projectName, e);
}
}
代码示例来源:origin: foxinmy/weixin4j
@Override
public void close() {
try {
EntityUtils.consume(httpResponse.getEntity());
httpResponse.close();
} catch (IOException ex) {
;
}
}
}
代码示例来源:origin: GeoWebCache/geowebcache
@Test
public void testPutGlobalReadOnly() throws Exception {
// PUT a value that is read-only
final String globalUpdate = "<global><location>foobar</location></global>";
CloseableHttpResponse response =
handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate);
assertEquals(400, response.getStatusLine().getStatusCode());
response.close();
}
代码示例来源:origin: medcl/elasticsearch-analysis-ik
get.setConfig(rc);
try {
response = httpclient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
if (response.getEntity().getContentType().getValue().contains("charset=")) {
String contentType = response.getEntity().getContentType().getValue();
charset = contentType.substring(contentType.lastIndexOf("=") + 1);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String line;
while ((line = in.readLine()) != null) {
response.close();
return buffer;
response.close();
} catch (ClientProtocolException e) {
logger.error("getRemoteWords {} error", e, location);
代码示例来源:origin: alibaba/jstorm
private boolean httpPost(String url, String msg) {
boolean ret = false;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
try {
HttpPost request = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("name", monitorName));
nvps.add(new BasicNameValuePair("msg", msg));
request.setEntity(new UrlEncodedFormEntity(nvps));
response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
LOG.info(EntityUtils.toString(entity));
}
EntityUtils.consume(entity);
ret = true;
} catch (Exception e) {
LOG.error("Exception when sending http request to ali monitor", e);
} finally {
try {
if (response != null)
response.close();
httpClient.close();
} catch (Exception e) {
LOG.error("Exception when closing httpclient", e);
}
}
return ret;
}
代码示例来源:origin: kaaproject/kaa
if (!Thread.currentThread().isInterrupted()) {
LOG.debug("Executing request {}", method.getRequestLine());
CloseableHttpResponse response = httpClient.execute(method);
try {
LOG.debug("Received {}", response.getStatusLine());
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
responseDataRaw = getResponseBody(response, verifyResponse);
response.close();
method = null;
代码示例来源:origin: floragunncom/search-guard
public HttpResponse(CloseableHttpResponse inner) throws IllegalStateException, IOException {
super();
this.inner = inner;
final HttpEntity entity = inner.getEntity();
if(entity == null) { //head request does not have a entity
this.body = "";
} else {
this.body = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
}
this.header = inner.getAllHeaders();
this.statusCode = inner.getStatusLine().getStatusCode();
this.statusReason = inner.getStatusLine().getReasonPhrase();
inner.close();
}
代码示例来源:origin: apache/incubator-gobblin
httpPost.setHeaders(new Header[]{contentType, requestType});
CloseableHttpResponse response = this.httpClient.execute(httpPost);
return new AzkabanClientStatus.SUCCESS();
} finally {
response.close();
代码示例来源:origin: mrdear/JavaWEB
/**
* 处理返回的请求,拿到返回内容
* @param httpResponse 要处理的返回
* @return 返回的内容
*/
private static String consumeResponse(CloseableHttpResponse httpResponse,String encording){
String result = null;
try {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity,encording);
EntityUtils.consume(httpEntity);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpResponse.close();
} catch (IOException ignored) {
}
}
return result;
}
}
代码示例来源:origin: alibaba/canal
HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(config);
CloseableHttpResponse response = httpclient.execute(httpGet, context);
try {
int statusCode = response.getStatusLine().getStatusCode();
long end = System.currentTimeMillis();
long cost = end - start;
return EntityUtils.toByteArray(response.getEntity());
} else {
String errorMsg = EntityUtils.toString(response.getEntity());
throw new RuntimeException("requestGet remote error, url=" + uri.toString() + ", code=" + statusCode
+ ", error msg=" + errorMsg);
response.close();
httpGet.releaseConnection();
代码示例来源:origin: scouter-project/scouter
@ConfigDesc("request http get")
@ParamDesc("String url, Map<String, String> paramMap, int timeoutMillis")
public void httpGet(String _url, Map<String, String> paramMap, int timeoutMillis) {
CloseableHttpResponse response = null;
try {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(1000)
.setConnectionRequestTimeout(1000)
.setSocketTimeout(timeoutMillis).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpGet httpGet = new HttpGet(_url + "?" + getParamsString(paramMap));
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
}
}
代码示例来源:origin: medcl/elasticsearch-analysis-ik
try {
response = httpclient.execute(head);
if(response.getStatusLine().getStatusCode()==200){
eTags = response.getLastHeader("ETag")==null?null:response.getLastHeader("ETag").getValue();
}else if (response.getStatusLine().getStatusCode()==304) {
logger.info("remote_ext_dict {} return bad code {}" , location , response.getStatusLine().getStatusCode() );
try {
if (response != null) {
response.close();
代码示例来源:origin: liyiorg/weixin-popular
CloseableHttpResponse httpResponse = LocalHttpClient.execute(httpUriRequest);
try {
int status = httpResponse.getStatusLine().getStatusCode();
if (status == 200) {
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
return ImageIO.read(new ByteArrayInputStream(bytes));
} finally {
try {
httpResponse.close();
} catch (IOException e) {
logger.error("", e);
代码示例来源:origin: apache/incubator-gobblin
/**
* Cancel a flow by execution id.
*/
public AzkabanClientStatus cancelFlow(int execId) {
try {
refreshSession();
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair(AzkabanClientParams.AJAX, "cancelFlow"));
nvps.add(new BasicNameValuePair(AzkabanClientParams.SESSION_ID, this.sessionId));
nvps.add(new BasicNameValuePair(AzkabanClientParams.EXECID, String.valueOf(execId)));
Header contentType = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
Header requestType = new BasicHeader("X-Requested-With", "XMLHttpRequest");
HttpGet httpGet = new HttpGet(url + "/executor?" + URLEncodedUtils.format(nvps, "UTF-8"));
httpGet.setHeaders(new Header[]{contentType, requestType});
CloseableHttpResponse response = this.httpClient.execute(httpGet);
try {
handleResponse(response);
return new AzkabanClientStatus.SUCCESS();
} finally {
response.close();
}
} catch (Exception e) {
return new AzkabanClientStatus.FAIL("", e);
}
}
代码示例来源:origin: com.foxinmy/weixin4j-base
@Override
public void close() {
try {
EntityUtils.consume(httpResponse.getEntity());
httpResponse.close();
} catch (IOException ex) {
;
}
}
}
代码示例来源:origin: alibaba/canal
HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(config);
response = httpclient.execute(httpGet, context);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else {
String errorMsg = EntityUtils.toString(response.getEntity());
throw new RuntimeException("requestGet remote error, url=" + uri.toString() + ", code=" + statusCode
+ ", error msg=" + errorMsg);
if (response != null) {
try {
response.close();
} catch (IOException e) {
内容来源于网络,如有侵权,请联系作者删除!