java - Apache HttpGet url编码问题(加号,+)

标签 java android url-encoding apache-httpclient-4.x

我正在使用 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/

相关文章:

java - OpenCV-Android : Import . png 保留空白区域的透明度

java - Eclipse 类型建议从 3.4 更改为 3.5 - 可配置?

java - 将 HTTPPost 请求中的 JSON 数据从 Java 传递到 ASP.NET MVC

ios - 为什么 URL 片段之前的 # char 由 stringByAddingPercentEscapesUsingEncoding 编码?

javascript - Node.js:以 win1251 字符集发送 http 请求

Spring Boot 2 : Plus (+) sign is not getting encoded in query param

Java:何时使用检查异常、非检查异常或断言

java - 从内部存储中获取图像时出现 FileNotFoundException

java - NoClassDefFound错误: Logcat reads $1 indicating error on inner class but I have none

android - 从未传递消息的 SMS 内容 URI 中获取消息