【问题标题】:Url Redirection error if url contains reserved characters如果 url 包含保留字符,则 URL 重定向错误
【发布时间】:2012-09-12 23:52:02
【问题描述】:

我正在尝试访问特定的网址

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope("abc.com", 443),
                new UsernamePasswordCredentials("user", "Passwd"));

HTTPHelper http = new HTTPHelper(httpclient);

http.get("http://abc.com/**aaa**/w/api.php?param=timestamp%7Cuser&format=xml");

%7C= |

在内部将我重定向到以下网址

http://abc.com/**bbb**/w/api.php?param=timestamp%257Cuser&format=xml

因此我无法获得正确的输出...

| ==>%7C

%==> %25

%7C == %257C

我希望查询为timestamp|user 但由于循环重定向,它变成了timestamp%7Cuser 有什么办法可以避免吗?

我也编写了自己的自定义重定向策略

httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
                boolean isRedirect = false;
                try {
                    isRedirect = super.isRedirected(request, response, context);
                    LOG.info(response.getStatusLine().getStatusCode());
                    LOG.info(request.getRequestLine().getUri().replaceAll("%25", "%"));
                } catch (ProtocolException e) {
                    e.printStackTrace();
                }
                if (!isRedirect) {
                    int responseCode = response.getStatusLine().getStatusCode();
                    if (responseCode == 301 || responseCode == 302) {
                        return true;
                    }
                }
                return isRedirect;
            }
        });

但我不确定如何将 %25C 替换为来自重定向 url 的 %7C

【问题讨论】:

    标签: http url redirect httpclient mediawiki


    【解决方案1】:

    看起来网站的 URL 重写规则被破坏了。如果这不是您的网站,您可能需要联系其维护人员并告知他们该问题。

    与此同时,您是否有某些原因不能直接使用目标 URL(即http://abc.com/**bbb**/w/api.php?...)来避免重定向?

    【讨论】:

    • 感谢 IImari 的回复...但我无法控制该网站..我正在根据来自该网站的信息构建 url,该信息为我提供了 url abc.com**aaa **/.... 我可以通过编写自己的自定义重定向处理程序来做点什么吗??
    • 您可以尝试这样做,但老实说,您只是在编写一个 kluge 来解决该特定站点被破坏的特定方式。您的自定义处理程序不太可能对任何其他站点有用(它甚至可能会破坏其中的一些站点),并且如果损坏的站点改变了它们的重写规则,它甚至不能保证它会继续工作。因此,既然您无论如何都要实施特定于站点的 hack,为什么不添加一些代码,以便当站点告诉您使用 abc.com/**aaa** 时,您将其替换为 abc.com/**bbb**
    【解决方案2】:

    只有http.get("http://abc.com/**aaa**/w/api.php?param=timestamp|user&format=xml"); 有效吗?

    “内部将我重定向到以下网址”是什么意思?就像您的网址被再次编码一样。您可以在 HTTPHelper 中发布代码吗?我下面的测试代码工作正常。

    客户代码:

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://localhost:8080/test/myservlet?param=timestamp%7Cuser&format=xml");
    client.execute(get);
    

    Servlet 代码:

    protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
    
            String param = request.getParameter("param"); //get timestamp|user
            String format = request.getParameter("format");
    
        }
    

    【讨论】:

    • 不。它正在抛出 IllegalArgumentException
    • 主机“abc.com/**aaa**”被永久移动到其他服务器。我从它那里得到 301 响应,然后它被重定向到“abc.com/**bbb**”。在重定向时,url 再次被编码。我正在尝试拦截重定向并在中间再次进行 url 解码。但不能这样做..
    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2012-10-05
    相关资源
    最近更新 更多