HttpClient 2.0。参数“编码”
我必须使用 HttpClient 2.0(不能使用任何更新的版本),并且我遇到了下一个问题。当我使用该方法(在这种情况下为 post)时,它将参数“编码”为十六进制 ASCII 代码,并且“空格”变成“+”(接收者不想要的东西)。
有谁知道有办法避免吗?
多谢。
I have to use HttpClient 2.0 (can not use anything newer), and I am running into the next issue. When I use the method (post, in that case), it "codify" the parameters to the Hexadecimal ASCII code, and the "spaces" turned into "+" (something that the receiver don't want).
Does anyone know a way to avoid it?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
理论上,您可以通过手动构造包含参数的查询字符串或请求正文来避免这种情况。
但这将是一件坏事,因为 HTML、HTTP、URL 和 URI 规范都要求对请求参数中的保留字符进行编码。如果您违反了这一点,您可能会发现服务器端 HTTP 堆栈、代理等会拒绝您的请求,因为您的请求无效,或者以其他方式行为不当。
处理此问题的正确方法是执行以下操作之一:
如果服务器是用 Java EE 技术实现的,请使用相关的 servlet API 方法(例如
ServletRequest.getParam(...)) 获取请求参数。这些将为您处理任何解码。
如果参数是 URL 查询字符串的一部分,您可以实例化 Java URL 或 URI 对象,并使用 getter 返回删除了编码的查询。
如果您的服务器以其他方式实现(或者您需要自己取消选择请求 URL 的查询字符串或 POST 数据),则使用 URLDecoder.decode 或等效方法删除 % 编码并替换+ 的...在您弄清楚查询和参数边界等在哪里之后。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
甚至您的浏览器也会这样做,将空格字符转换为+。请参阅此处 http://download.oracle.com /javase/1.5.0/docs/api/java/net/URLEncoder.html
它对 URL 进行编码,转换为类似 UTF-8 的字符串。
另请参阅此处 http://www.w3.org /TR/html4/interact/forms.html#h-17.13.4.1
如果您不想编码,请回答您的问题。我想, URLDecoder.decode 将帮助您撤消编码的字符串。
Even your browser does that, converting space character into +. See here http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html
It encodes URL, converts to UTF-8 like string.
Also, see here http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
To answer your question, if you do not want to encode. I guess, URLDecoder.decode will help you to undo the encoded string.