用于修复格式错误的URI的Scala或Java库
有没有人知道一个好的Scala或Java库可以修复格式错误的URI中的常见问题,例如包含应该转义但不是转义的字符?我已经测试了一些库,包括现在遗留的HTTPClient,但没有发现任何可行的解决方案。通常,我在这种类型的构造上已经取得了足够的成功:用于修复格式错误的URI的Scala或Java库,java,scala,uri,malformed,Java,Scala,Uri,Malformed,有没有人知道一个好的Scala或Java库可以修复格式错误的URI中的常见问题,例如包含应该转义但不是转义的字符?我已经测试了一些库,包括现在遗留的HTTPClient,但没有发现任何可行的解决方案。通常,我在这种类型的构造上已经取得了足够的成功: /** * Tries to construct an url by breaking it up into its smallest elements * and encode each component individually using
/**
* Tries to construct an url by breaking it up into its smallest elements
* and encode each component individually using the full URI constructor:
*
* foo://example.com:8042/over/there?name=ferret#nose
* \_/ \______________/\_________/ \_________/ \__/
* | | | | |
* scheme authority path query fragment
*/
public URI parseUrl(String s) throws Exception {
URL u = new URL(s);
return new URI(
u.getProtocol(),
u.getAuthority(),
u.getPath(),
u.getQuery(),
u.getRef());
}
可与以下程序结合使用。它会反复解码URL
,直到解码的字符串不变,这对防止双重编码非常有用。注意,为了简单起见,此示例没有任何故障保护等功能
public String urlDecode(String url, String encoding) throws UnsupportedEncodingException, IllegalArgumentException {
String result = URLDecoder.decode(url, encoding);
return result.equals(url) ? result : urlDecode(result, encoding);
}
我建议不要使用
java.net.urlcoder
对URI进行百分比编码。尽管名称不同,但它对URL编码并不适用,因为它不符合标准,而是编码为应用程序/x-www-form-urlencoded
MIME格式()
对于Scala中的URI编码,我建议使用spray http中的类。是另一种选择(免责声明:我是作者)