HttpClient 2.0。参数“编码”

发布于 10-18 04:13 字数 154 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主2024-10-25 04:13:47

甚至您的浏览器也会这样做,将空格字符转换为+。请参阅此处 http://download.oracle.com /javase/1.5.0/docs/api/java/net/URLEncoder.html

它对 URL 进行编码,转换为类似 UTF-8 的字符串。

对字符串进行编码时,适用以下规则:

  • 字母数字字符“a”到“z”、“A”到“Z”以及“0”到“9”保持不变。
  • 特殊字符“.”、“-”、“*”和“_”保持不变。
  • 空格字符“ ”被转换为加号“+”。
  • 所有其他字符都是不安全的,首先使用某种编码方案将其转换为一个或多个字节。然后每个字节由 3 个字符的字符串“%xy”表示,其中 xy 是该字节的两位十六进制表示形式。推荐使用的编码方案是 UTF-8。但是,出于兼容性原因,如果未指定编码,则使用平台的默认编码。

另请参阅此处 http://www.w3.org /TR/html4/interact/forms.html#h-17.13.4.1

  1. 控件名称和值被转义。空格字符被替换为+',然后保留字符被转义,如[RFC1738],第2.2节中所述:非字母数字字符被替换为%HH',一个百分号和两个十六进制数字代表字符的 ASCII 码。换行符表示为“CR LF”对(即“%0D%0A”)。

  2. 控件名称/值按照它们在文档中出现的顺序列出。名称与值之间用 =' 分隔,名称/值对之间用&' 分隔。


如果您不想编码,请回答您的问题。我想, 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.

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.

Also, see here http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1

  1. Control names and values are escaped. Space characters are replaced by +', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').

  2. The control names/values are listed in the order they appear in the document. The name is separated from the value by =' and name/value pairs are separated from each other by&'.


To answer your question, if you do not want to encode. I guess, URLDecoder.decode will help you to undo the encoded string.

汹涌人海2024-10-25 04:13:47

理论上,您可以通过手动构造包含参数的查询字符串或请求正文来避免这种情况。

但这将是一件坏事,因为 HTML、HTTP、URL 和 URI 规范都要求对请求参数中的保留字符进行编码。如果您违反了这一点,您可能会发现服务器端 HTTP 堆栈、代理等会拒绝您的请求,因为您的请求无效,或者以其他方式行为不当。

处理此问题的正确方法是执行以下操作之一:

  • 如果服务器是用 Java EE 技术实现的,请使用相关的 servlet API 方法(例如 ServletRequest.getParam(...)) 获取请求参数。这些将为您处理任何解码。

  • 如果参数是 URL 查询字符串的一部分,您可以实例化 Java URL 或 URI 对象,并使用 getter 返回删除了编码的查询。

  • 如果您的服务器以其他方式实现(或者您需要自己取消选择请求 URL 的查询字符串或 POST 数据),则使用 URLDecoder.decode 或等效方法删除 % 编码并替换+ 的...在您弄清楚查询和参数边界等在哪里之后。

You could in theory avoid this by constructing the query string or request body containing parameters by hand.

But this would be a bad thing to do, because the HTML, HTTP, URL and URI specs all mandate that reserved characters in request parameters are encoded. And if you violate this, you may find that server-side HTTP stacks, proxies and so on reject your requests as invalid, or misbehave in other ways.

The correct way to deal with this issue is to do one of the following:

  • If the server is implemented in Java EE technology, use the relevant servlet API methods (e.g. ServletRequest.getParam(...)) to fetch the request parameters. These will take care of any decoding for you.

  • If the parameters are part of a URL query string, you can instantiate a Java URL or URI object and use the getter to return you the query with the encoding removed.

  • If your server is implemented some other way (or if you need to unpick the request URL's query string or POST data yourself), then use URLDecoder.decode or equivalent to remove the % encoding and replace +'s ... after you have figured out where the query and parameter boundaries, etc are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文