本文整理匯總了Java中org.apache.http.client.methods.HttpRequestBase.releaseConnection方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpRequestBase.releaseConnection方法的具體用法?Java HttpRequestBase.releaseConnection怎麽用?Java HttpRequestBase.releaseConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.methods.HttpRequestBase
的用法示例。
在下文中一共展示了HttpRequestBase.releaseConnection方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getResponseAsString
import org.apache.http.client.methods.HttpRequestBase; //導入方法依賴的package包/類
public static Optional<String> getResponseAsString(HttpRequestBase httpRequest, HttpClient client) {
Optional<String> result = Optional.empty();
final int waitTime = 60000;
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(waitTime).setConnectTimeout(waitTime)
.setConnectionRequestTimeout(waitTime).build();
httpRequest.setConfig(requestConfig);
result = Optional.of(client.execute(httpRequest, responseHandler));
} catch (HttpResponseException httpResponseException) {
LOG.error("getResponseAsString(): caught 'HttpResponseException' while processing request <{}> :=> <{}>", httpRequest,
httpResponseException.getMessage());
} catch (IOException ioe) {
LOG.error("getResponseAsString(): caught 'IOException' while processing request <{}> :=> <{}>", httpRequest, ioe.getMessage());
} finally {
httpRequest.releaseConnection();
}
return result;
}
示例2: doRequest
import org.apache.http.client.methods.HttpRequestBase; //導入方法依賴的package包/類
private <T> HttpResponse<T> doRequest(HttpRequest request, Class<T> responseClass) {
HttpRequestBase requestObj = prepareRequest(request, false);
org.apache.http.HttpResponse response;
try {
response = syncClient.execute(requestObj);
HttpResponse<T> httpResponse = new HttpResponse<>(response, responseClass, objectMapper);
requestObj.releaseConnection();
return httpResponse;
} catch (Exception e) {
throw new RestClientException(e);
} finally {
requestObj.releaseConnection();
}
}
示例3: save
import org.apache.http.client.methods.HttpRequestBase; //導入方法依賴的package包/類
@Override public HttpResponse save (final IlluminatiEsModel entity) {
final HttpRequestBase httpPutRequest = new HttpPut(entity.getEsUrl(this.esUrl));
if (entity.isSetUserAuth() == true) {
try {
httpPutRequest.setHeader("Authorization", "Basic " + entity.getEsAuthString());
} catch (Exception ex) {
this.logger.error("Sorry. something is wrong in encoding es user auth info. ("+ex.toString()+")");
}
}
((HttpPut) httpPutRequest).setEntity(this.getHttpEntity(entity));
HttpResponse httpResponse = null;
try {
httpResponse = this.httpClient.execute(httpPutRequest);
} catch (IOException e) {
this.logger.error("Sorry. something is wrong in Http Request. ("+e.toString()+")");
} finally {
httpPutRequest.releaseConnection();
}
if (httpResponse == null) {
httpResponse = getHttpResponseByData(this.errorCode, "Sorry. something is wrong in Http Request.");
}
return httpResponse;
}
示例4: res
import org.apache.http.client.methods.HttpRequestBase; //導入方法依賴的package包/類
private String res(HttpRequestBase method) {
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse response = null;
String content = RESPONSE_CONTENT;
try {
response = client.execute(method, context);//執行GET/POST請求
HttpEntity entity = response.getEntity();//獲取響應實體
if(entity!=null) {
Charset charset = ContentType.getOrDefault(entity).getCharset();
content = EntityUtils.toString(entity, charset);
EntityUtils.consume(entity);
}
} catch(ConnectTimeoutException cte) {
LOG.error("請求連接超時,由於 " + cte.getLocalizedMessage());
cte.printStackTrace();
} catch(SocketTimeoutException ste) {
LOG.error("請求通信超時,由於 " + ste.getLocalizedMessage());
ste.printStackTrace();
} catch(ClientProtocolException cpe) {
LOG.error("協議錯誤(比如構造HttpGet對象時傳入協議不對(將'http'寫成'htp')or響應內容不符合),由於 " + cpe.getLocalizedMessage());
cpe.printStackTrace();
} catch(IOException ie) {
LOG.error("實體轉換異常或者網絡異常, 由於 " + ie.getLocalizedMessage());
ie.printStackTrace();
} finally {
try {
if(response!=null) {
response.close();
}
} catch(IOException e) {
LOG.error("響應關閉異常, 由於 " + e.getLocalizedMessage());
}
if(method!=null) {
method.releaseConnection();
}
}
return content;
}
示例5: getResponseContent
import org.apache.http.client.methods.HttpRequestBase; //導入方法依賴的package包/類
private String getResponseContent(String url, HttpRequestBase request) throws Exception {
HttpResponse response = null;
try {
response = this.getHttpClient(2000,2000).execute(request);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new Exception("got an error from HTTP for url : " + URLDecoder.decode(url, "UTF-8"),e);
} finally {
if(response != null){
EntityUtils.consumeQuietly(response.getEntity());
}
request.releaseConnection();
}
}