URL 编码引号和空格
我有一些使用 JavaScript 编码的查询文本,但我遇到了一个用例,我可能必须在服务器端对相同的文本进行编码,并且发生的编码并不相同。我需要它是一样的。这是一个例子。
我在搜索框中输入“我喜欢食物”
,然后点击搜索
按钮。 JavaScript 将其编码为 %22I%20like%20food%22
假设我得到的值与服务器端请求对象上的字符串相同。它看起来像这样: "\"I like food\""
当我使用 HttpUtility.UrlEncode(value)
时,结果是 "%22I+like +食物%22”
。如果我使用 HttpUtility.UrlPathEncode(value)
,结果是 "\"I%20like%20food\""
所以 UrlEncode
正在编码我的引号,但使用 +
字符作为空格。 UrlPathEncode
正在对我的空格进行编码,但没有对我的转义引号进行编码。
我真的需要它来完成这两件事,否则搜索代码完全让我厌烦(而且我无法控制搜索代码)。
尖端?
I have some query text that is being encoded with JavaScript, but I've encountered a use case where I might have to encode the same text on the server side, and the encoding that's happening is not the same. I need it to be the same. Here's an example.
I enter "I like food"
into the search box and hit the search
button. JavaScript encodes this as %22I%20like%20food%22
Let's say I get the same value as a string on a request object on the server side. It will look like this: "\"I like food\""
When I use HttpUtility.UrlEncode(value)
, the result is "%22I+like+food%22"
. If I use HttpUtility.UrlPathEncode(value)
, the result is "\"I%20like%20food\""
So UrlEncode
is encoding my quotes but is using the +
character for spaces. UrlPathEncode
is encoding my spaces but is not encoding my escaped quotes.
I really need it to do both, otherwise the Search code completely borks on me (and I have no control over the search code).
Tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UrlPathEncode
不会转义"
,因为它们不需要在路径组件中转义。Uri.EscapeDataString
应该做你想做的。UrlPathEncode
doesn't escape"
because they don't need to be escaped in path components.Uri.EscapeDataString
should do what you want.有几个选项可供您使用,最快的可能是使用 UrlEncode,然后执行 string.replace 将
+
字符与%20
交换。像这样的东西
There are a few options available to you, the fastest might be to use UrlEncode then do a string.replace to swap the
+
characters with%20
.Something like
将使用
%XX
格式对所有需要编码的字符进行编码,包括空格。Will encode all characters that need encoded using the
%XX
format, including space.