Apache HttpClient中循环重定向的bug详解
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-20 16:51:16 浏览: 评论:0
最近在使用Apache HttpClient4.x时发现如果出现循环重定向的话就会出现bug问题了,这样导致服务器无法正常访问,会一直跳来跳去了,下面我来给大家介绍解决此问题的方法.
在Apache HttpClient 4.0里有个循环重定向的bug,当服务器试图正常的从域名A跳转到B的时候就会发生,我是在 phpfensi.com 跳 www.phpfensi.com 的时候发现的,换过最新版也发现还是有这个问题.
跟踪了DefaultRequestDirector.java之后发现,它在内部创建了一个HttpRedirect对象来处理重定向,这个对象会重用你之前的HttpGet对象里的HTTP 头,导致这个bug的根源在于,连Host头也用了,这表示重定向到的新域名如果和老域名如果是同一组集群,那么服务器会试图重新发一个重定向请求,这样就造成了HttpClient抛出CircularRedirectException.
我是派生了DefaultRequestDirector类来改掉这个问题,代码如下:
- public class RedirectRequestDirector extends DefaultRequestDirector
- {
- RedirectRequestDirector(
- final HttpRequestExecutor requestExec,
- final ClientConnectionManager conman,
- final ConnectionReuseStrategy reustrat,
- final ConnectionKeepAliveStrategy kastrat,
- final HttpRoutePlanner rouplan,
- final HttpProcessor httpProcessor,
- final HttpRequestRetryHandler retryHandler,
- final RedirectHandler redirectHandler,
- final AuthenticationHandler targetAuthHandler,
- final AuthenticationHandler proxyAuthHandler,
- final UserTokenHandler userTokenHandler,
- final HttpParams params)
- {
- super(requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectHandler, targetAuthHandler, proxyAuthHandler, userTokenHandler, params);
- }
- @Override
- protected RoutedRequest handleResponse(RoutedRequest roureq,
- HttpResponse response,
- HttpContext context)
- throws HttpException, IOException
- {
- RoutedRequest req = super.handleResponse(roureq, response, context);
- if(req != null)
- {
- String redirectTarget = req.getRoute().getTargetHost().getHostName();
- req.getRequest().getOriginal().setHeader("Host", redirectTarget);//开源代码phpfensi.com
- }
- return req;
- }
- }
这里修改了Host头为试图重定向的主机名,还需要修改 DefaultHttpClient,代码如下:
- public class RedirectHttpClient extends DefaultHttpClient
- {
- @Override
- protected RequestDirector createClientRequestDirector(
- final HttpRequestExecutor requestExec,
- final ClientConnectionManager conman,
- final ConnectionReuseStrategy reustrat,
- final ConnectionKeepAliveStrategy kastrat,
- final HttpRoutePlanner rouplan,
- final HttpProcessor httpProcessor,
- final HttpRequestRetryHandler retryHandler,
- final RedirectHandler redirectHandler,
- final AuthenticationHandler targetAuthHandler,
- final AuthenticationHandler proxyAuthHandler,
- final UserTokenHandler stateHandler,
- final HttpParams params) {
- return new RedirectRequestDirector(
- requestExec,
- conman,
- reustrat,
- kastrat,
- rouplan,
- httpProcessor,
- retryHandler,
- redirectHandler,
- targetAuthHandler,
- proxyAuthHandler,
- stateHandler,
- params);
- }
- }
现在就能正常处理跳转了.
Tags: Apache HttpClient 重定向bug
相关文章
- ·在apache下限制每个虚拟主机的并发数(2013-11-14)
- ·APACHE禁止图片盗链(2013-11-14)
- ·Apache下的虚拟主机设置(2013-11-14)
- ·Apache中禁止IP段,在httpd.conf中的写法(2013-11-14)
- ·用Apache的rewrite生成伪静态页面(2013-11-14)
- ·HTTP/Apache 错误代码汇总(2013-11-14)
- ·Apache性能优化技巧(2013-11-22)
- ·用Session代替Apache服务器验证(2013-11-22)
- ·Apache中httpd.conf的中文件详解(2013-11-22)
- ·Apache中.Htaccess文件的功能写法(2013-11-22)
- ·如何让apache支持.htaccess并设置404错误页(2013-11-22)
- ·apache 虚拟主机配置[别名配置方法](2013-11-22)
- ·apache 服务器网站不能访问(2013-11-22)
- ·apache教程:.htaccess用法(2013-11-22)
- ·Apache二级域名配置方法(2013-11-22)
- ·apache 封IP基础教程(2013-11-22)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)