HttpClient的CircularRedirectException异常解决办法

HttpClient的CircularRedirectException解决办法

org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at com.baidusplider.webbaidusplider.Bingsplidertest.getSearchInfoHtml(Bingsplidertest.java:79)
at com.baidusplider.webbaidusplider.Bingsplidertest.main(Bingsplidertest.java:151)
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to ‘http://cn.bing.com/search?q=LOL
at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:177)
at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:221)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:122)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
… 4 more
处理办法:设置HttpClient参数,在new一个get请求之后写上httpGet.getParams().setParameter(“http.protocol.allow-circular-redirects”, true);
‘http.protocol.allow-circular-redirects’:定义环形重定向(重定向到相同路径)是否被允许。HTTP规范在环形重定向没有足够清晰的允许表述,因此这作为可选的是可以开启的。这个参数期望得到一个java.lang.Boolean类型的值。如果这个参数没有被设置,那么环形重定向就不允许。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 HttpClient 进行网络请求时,可能会遇到超时异常。处理超时异常的方法有以下几种: 1. 设置超时时间:在创建 HttpClient 实例时,可以设置连接超时时间和读取超时时间,以确保请求在规定时间内得到响应。例如: ``` RequestConfig config = RequestConfig.custom() .setConnectTimeout(5000) // 连接超时时间为5秒 .setSocketTimeout(5000) // 读取超时时间为5秒 .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(config) .build(); ``` 2. 使用连接池:使用连接池可以提高 HttpClient 的性能和稳定性,同时也可以减少超时异常的发生。例如: ``` PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setMaxTotal(100); // 最大连接数为100 connManager.setDefaultMaxPerRoute(20); // 每个路由的最大连接数为20 CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connManager) .build(); ``` 3. 重试机制:在请求失败时,可以进行重试,以增加请求成功的概率。例如: ``` HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 3) { // 最多重试3次 return false; } if (exception instanceof InterruptedIOException) { // 超时异常 return true; } if (exception instanceof UnknownHostException) { // 未知主机异常 return false; } if (exception instanceof ConnectTimeoutException) { // 连接超时异常 return true; } if (exception instanceof SSLException) { // SSL异常 return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // 幂等请求 return true; } return false; } }; CloseableHttpClient httpClient = HttpClients.custom() .setRetryHandler(retryHandler) .build(); ``` 以上是处理 HttpClient 超时异常的几种方法,具体选择哪种方法,需要根据实际情况进行判断。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值