本文整理汇总了Java中org.apache.http.impl.client.DefaultHttpClient.setHttpRequestRetryHandler方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultHttpClient.setHttpRequestRetryHandler方法的具体用法?Java DefaultHttpClient.setHttpRequestRetryHandler怎么用?Java DefaultHttpClient.setHttpRequestRetryHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.client.DefaultHttpClient
的用法示例。
在下文中一共展示了DefaultHttpClient.setHttpRequestRetryHandler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHttpClient
import org.apache.http.impl.client.DefaultHttpClient; //导入方法依赖的package包/类
/**
* Create a thread-safe client. This client does not do redirecting, to allow us to capture
* correct "error" codes.
*
* @return HttpClient
*/
public final DefaultHttpClient createHttpClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(params, true);
// Turn off stale checking. Our connections break all the time anyway,
// and it's not worth it to pay the penalty of checking every time.
HttpConnectionParams.setStaleCheckingEnabled(params, false);
// Default connection and socket timeout of 30 seconds. Tweak to taste.
HttpConnectionParams.setConnectionTimeout(params, 10*1000);
HttpConnectionParams.setSoTimeout(params, 20*1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
ConnManagerParams.setTimeout(params, 5 * 1000);
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(50));
ConnManagerParams.setMaxTotalConnections(params, 200);
// Sets up the http part of the service.
final SchemeRegistry supportedSchemes = new SchemeRegistry();
// Register the "http" protocol scheme, it is required
// by the default operator to look up socket factories.
final SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params,
supportedSchemes);
DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));
return httpClient;
}
示例2: getHttpClient
import org.apache.http.impl.client.DefaultHttpClient; //导入方法依赖的package包/类
public HttpClient getHttpClient()
{
DefaultHttpClient client = new DefaultHttpClient(cm);
Integer socketTimeout = 10000;
Integer connectionTimeout = 10000;
final int retryTime = 3;
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, false);
client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 1024 * 1024);
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler()
{
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context)
{
if (executionCount >= retryTime)
{
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException)
{
// Timeout
return false;
}
if (exception instanceof UnknownHostException)
{
// Unknown host
return false;
}
if (exception instanceof ConnectException)
{
// Connection refused
return false;
}
if (exception instanceof SSLException)
{
// SSL handshake exception
return false;
}
HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent)
{
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
client.setHttpRequestRetryHandler(myRetryHandler);
return client;
}