本文整理匯總了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;
}