本文主要应用了OkHttp的Interceptor来实现自定义重试次数 虽然OkHttp自带retryOnConnectionFailure(true)方法可以实现重试,但是不支持自定义重试次数,所以有时并不能满足我们的需求. #1.自定义重试拦截器: /** * 重试拦截器 */ public class RetryIntercepter implements Interceptor { public int maxRetry;//最大重试次数 ;//假如设置为3次重试的话,则最大可能请求4次
设置请求时的重试规则 import requests from requests.adapters import HTTPAdapter s = requests.Session() a = HTTPAdapter(max_retries=3) b = HTTPAdapter(max_retries=3) #将重试规则挂载到http和https请求 s.mount('http://', a) s.mount('https://', b) 请求Url 上面设置完毕后,通过改Session的请求就可
HTTP_CACHE_FILENAME为缓存地址根路径: private final String HTTP_CACHE_FILENAME = "HttpCache"; private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { R
import urllib.request def download(url, num_retries=2): print('Downloading:', url) try: html = urllib.request.urlopen(url).read() except urllib.URLError as e: print('Download error:' % e.reason) html = None if num_retries > 0: if hasattr(e, 'code') a