【发布时间】:2018-06-20 10:35:37
【问题描述】:
我正在尝试使用作为 foo & bar 的查询参数以及编码后的 URL 来访问 Web 服务 URL。为了实现编码,我使用的是 Apache URIBuilder。代码如下:
URIBuilder ub = new URIBuilder("http://example.com/query").setParameter("q", "foo & bar");
URI uri = ub.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
我得到以下输出:http://example.com/query?q=+foo+%2526+bar
我是这个 JAR 文件的新手,有 2 个问题:
为什么查询参数中的空格被替换为“+”号而不是 %20 特殊字符。
为什么查询参数中的“&”符号会被编码两次以及如何避免。
【问题讨论】:
-
第二个问题请看这里stackoverflow.com/questions/9292865/what-is-url-encoding-2526。并且 '+' 是空格字符的有效替换,更多信息在这里 (stackoverflow.com/questions/2678551/…)。
-
谢谢米哈尔。抱歉延迟回复。