请求 URL 中的括号是合法的,但在 URI (Java) 中不合法? [英] Brackets in a Request URL are legal but not in a URI (Java)?

查看:38
本文介绍了请求 URL 中的括号是合法的,但在 URI (Java) 中不合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,URI 路径中不允许使用括号.

Apparently brackets are not allowed in URI paths.

我不确定这是否是 Tomcat 问题,但我收到的请求的路径包含 ].

I'm not sure if this is a Tomcat problem but I'm getting request with paths that contains ].

换句话说

request.getRequestURL() == "http://localhost:8080/a]b"
request.getRequestURI() == "/a]b"

顺便说一句,getRequestURL() 和 URI 通常被转义,即对于 http://localhost:8080/a b

BTW getRequestURL() and URI are generally escaped ie for http://localhost:8080/a b

request.getRequestURL() == "http://localhost:8080/a%20b"

因此,如果您尝试这样做:

So if you try to do:

new URI("http://localhost:8080/a]b")
new URI(request.getRequestURL())

它将因 URI 解析异常而失败.如果我转义将使 %20 双重转义的路径.

It will fail with a URI parsing exception. If I escape the path that will make the %20 double escaped.

如何将 Servlet 请求 URL 转换为 URI?

推荐答案

Java 的 URI 似乎非常严格,需要转义 排除的 US-ASCII 字符集.

Java's URI appears to be very strict and requires escaping for the Excluded US-ASCII Charset.

为了解决这个问题,我只对那些减去 '%''#' 的字符进行编码,因为 URL 可能已经包含这些字符.我使用了 Http Clients URI utils,由于某种原因它不在 HttpComponents 中.

To fix this I encode those and only those characters minus the '%' and '#' as the URL may already contain those character. I used Http Clients URI utils which for some reason is not in HttpComponents.

private static BitSet badUriChars = new BitSet(256);
static {
    badUriChars.set(0, 255, true);
    badUriChars.andNot(org.apache.commons.httpclient.URI.unwise);
    badUriChars.andNot(org.apache.commons.httpclient.URI.space);
    badUriChars.andNot(org.apache.commons.httpclient.URI.control);
    badUriChars.set('<', false);
    badUriChars.set('>', false);
    badUriChars.set('"', false);
}

public static URI toURIorFail(String url) throws URISyntaxException {
    URI uri = URIUtil.encode(url, badUriChars, "UTF-8");
    return new URI(uri);
}

以下是一些相关的 SO 帖子(更多内容):

Here are some related SO posts (more to come):

这篇关于请求 URL 中的括号是合法的,但在 URI (Java) 中不合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆