[英]How do you 'escape' a username:password pair in a connection string?
We have a connection string that is built like so: 我们有一个像这样构建的连接字符串:
http://${i.user}:${i.pass}@${i.host}:22
However, if that users password contains an '@', then we end up with a string like this: 但是,如果该用户密码包含“@”,那么我们最终得到一个如下字符串:
http://user:p@ss@1.2.3.4:22
This obviously throws the script off. 这显然会抛弃脚本。 Is there a way to escape that first @, or something to that effect?
有没有办法逃脱第一个@,或者那种效果?
Thanks 谢谢
用“%40”替换用户或密码中的每个“@”
Characters like "@" in the user info portion of a URI have to be escaped. 必须转义URI的用户信息部分中的“@”等字符。 When parsing such a URI, the URI consumer should unescape any escaped characters:
在解析这样的URI时,URI使用者应该忽略任何转义字符:
import java.net.URI;
import java.net.URISyntaxException;
public class URITest {
public static void main(String[] args) throws URISyntaxException {
URI uri = new URI("http", "user:p@ss", "example.com", -1,
null, null, null);
System.out.println(uri.toString());
URI uri2 = new URI(uri.toString());
System.out.println(uri2.getUserInfo());
}
}
On my system, this outputs: 在我的系统上,这输出:
http://user:p%40ss@example.com
user:p@ss
In the original question, it looks like you're not escaping the '@' when constructing the URI. 在最初的问题中,看起来你在构造URI时没有转义'@'。 In your comment to @leblma, it sounds like whatever is using the URI isn't unescaping the % sequence.
在你对@leblma的评论中,听起来像使用URI的任何事情都不会破坏%序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.