使用httpclient时如何覆盖方法?还需要什么?

问题描述

我们正在使用 httpclient 连接一个有很多重定向的网站。

在我们测试了我们的初始实现之后,我们得到了一个 ioexception,但是当我们尝试输出关于异常的信息时,比如 .getMessage 等,我们得到的只是一个 null。

然后我们尝试调试 webapp,使用 Eclipse 远程调试(web app 在 WebLogic 上运行),我们发现当它得到 ioexception 时,实际上有几个“层”的异常不是直接可见的。看来 httpclient 正在处理多个重定向“在幕后”并且不会触发我们的代码,即使该应用程序已配置为 LaxRedirect:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

从远程调试来看,似乎在其中一个“隐藏”重定向中,应用程序收到了:

java.net.URISyntaxException: Illegal character in query at index 316:   https://ZZZZZZZZZZZZZZZ.XXXXX.YYY:14430/oam/pages/consent.jsp?state=WDNvK0ltU2plY1J2dkJhSmJBZkV5dz09fi9SQXIrVVk0ZUZ0cW11WU80TEEyMmjibFFBVzNrZm5Jc3dGbHdINy9lTFY5OVhZRll0TzR5Z1hWNjExK3ZPQmZiTmNjcW9sVGVOWWdya3A1bHhmQ3k0REVSRytBbWwvSHlWbjVlUDk4RFoyeXBHZnBBenFOMU5CaDYzV3NTaGlt&scopes=UserProfile.me&client_id=TestClient|eventStack#10103:0,10125:0,10126:0,10205:0,10305:0,10201:0,10117:0|eventFlowControllerStack#ssoFlowController,|authn_try_count#_k=authn_try_count

看起来“非法字符”是“管道”字符(即“|”)。

然后我找到了这个帖子:

HttpClient redirecting to URL with spaces throwing exception

(参见“Drakes”中的帖子),其中讨论了使用自定义重定向策略并覆盖 createLocationURI() 方法,因此我们现在尝试这样做,将管道字符 ('|') 替换为 " %7C”。

所以我改变了:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

到:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new CleanRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

并做了一个小班:

class CleanUrlRedirectStrategy extends LaxRedirectStrategy
{

@Override
protected URI createLocationURI( final String orig ) throws ProtocolException
    {
      System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: Entering,orig=[" + orig + "]" );
     String fixed = orig.replaceAll("\\|","%7C"); 

     if ( ! orig.equals( fixed ) )
         {
         System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: FOUND PIPE CHaraCTER - Cleaned redirect URL from [" + orig + "] to [" + fixed + "]" );
     } else {
         System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: No PIPE characters found,so did not change orig!!!!" );
     }

 System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: about to RETURN,fixed=[" + fixed + "]" );

 return super.createLocationURI( fixed );
 }
 }// end CLASS CleanUrlRedirectStrategy

当我测试时,我可以看到日志输出,我可以看到在第一个 http.execute() 之后, CleanUrlRedirectStrategy.createLocationURI() 被调用了 3 次,但它没有找到任何管道字符(即有点在意料之中,因为从我们之前的测试来看,问题似乎是在第二次 http.execute() 完成时发生的。

我想我随后又看到了一次 http.execute() 尝试,但随后整个应用程序似乎都停止了(我在日志中没有看到任何进一步的输出)。

所以我想知道:我还需要做些什么才能使这种覆盖方法起作用?

“感觉”就像,当我将策略从松散策略更改为“干净”策略时,可能我的策略类不再遵循重定向

提前致谢!

吉姆

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)