繁体   English   中英

在查询字符串中用:创建Java URI

[英]Creating a URI in Java with a : in the query string

我正在尝试用Java创建URI,我的查询字符串中带有: 但是,无论我如何尝试创建URI,都会收到无效的响应。

new URI("http", "localhost:1181", "/stream.mjpg", "part1:part2", null).toString(); 给我http://localhost:1181/stream.mjpg?part1:part2 ,而查询字符串中的:没有被转义。

如果我逃跑创建URI之前的查询字符串,它逃脱%%3A ,给予%253A ,这是不正确。

new URI("http", "localhost:1181", "/stream.mjpg", "part1%3Apart2", null).toString(); 给出http://localhost:1181/stream.mjpg?part1%253Apart2

我的结果需要为http://localhost:1181/stream.mjpg?part1%3Apart2因为我的服务器要求:必须以查询字符串进行编码`

是否有我缺少的东西,还是必须手动创建查询字符串?

它不是很漂亮,但是您可以在查询部分使用URLEncoder:

String query = URLEncoder.encode("part1:part2", StandardCharsets.UTF_8);
// Required by server.
query = query.replace("+", "%20");

String uri =
    new URI("http", "localhost:1181", "/stream.mjpg", null, null)
    + "?" + query;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM