我正在使用 HttpClient 发送 GET 请求,但 +
未编码。
1.
如果我像这样传递未编码的 query
参数字符串
URI uri = new URI(scheme, host, path, query, null);
HttpGet get = new HttpGet(uri);
然后 +
符号不被编码,它在服务器上被接收为一个空格。 url 的其余部分编码良好。
2.如果我像这样在 query
字符串中编码参数
param = URLEncoder.encode(param,"UTF-8");
然后我在服务器上得到了一堆奇怪的符号,可能是因为url被编码了两次。
3.如果我像这样只用%B2
替换+
query = query.replaceAll("\\+","%B2");
然后在HttpClient执行GET时对%B2
进行编码
如何使用 Apache HttpClient 正确编码 Get 参数并确保 +
也被编码?
最佳答案
好的,解决方案是不要像这样创建 URI
URI uri = new URI(scheme, host, path, query, null);
应该这样创建
URIUtils.createURI(scheme, host, -1, path, query, null);
URIUtils 类的用途是
A collection of utilities for URIs, to workaround bugs within the class
暂无评论..........
关于java - Apache HttpGet url编码问题(加号,+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7315036/