stringByAddingPercentEscapesUsingEncoding 转义的字符是什么?
我不得不从 stringByAddingPercentEscapesUsingEncoding 切换到 CFURLCreateStringByAddingPercentEscapes 因为它不会转义问号(?)。我很好奇它到底转义了什么,以及部分转义与 RFC 3986 背后的基本原理。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 CFStringRef 时请小心不要在转换时泄漏内存。这是我处理拉丁字符和其他字符时想到的。我用它来转义我的参数,而不是整个 URL。根据您的使用情况,您可能需要添加或删除“escapeChars”中的字符,
我希望这会有所帮助。
Be careful not to leak memory on conversions when using CFStringRef. Here's what I came up with to work with Latin characters, and others. I use this to escape my parameters, not the entire URL. Depending on your use case, you may need to add or remove characters from "escapeChars"
I hope this helps.
我们创建了一些很好的类别来满足您的需求:
http://iosdevelopertips.com/networking/a-better-url-编码方法.html
http://www.cocoanetics.com/2009/08/url-encoding/< /a>
保留某些字符的基本原理超出了我的范围...除了说该函数的定义是:
使用给定编码返回接收器的表示形式以确定转换接收器所需的转义百分比 是合法的 URL 字符串。
为了完全正确,+ 和 & 是 URL 中的合法字符,而空格则不是。因此,该方法将正确转义空格,但留下 + 和 &完好无损的。
阅读 RFC2396 http://www.ietf.org/rfc/rfc2396.txt -定义了一组保留和非保留字符。我的猜测是,这些字符都没有被 stringByAddingPercentEscapesUsingEncoding 转义。
Some good categories have been created for doing just what you need:
http://iosdevelopertips.com/networking/a-better-url-encoding-method.html
http://www.cocoanetics.com/2009/08/url-encoding/
The rationale for leaving certain characters out is beyond me... except to say that the definition of the function is:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
To be completely correct, + and & are legal characters within a URL, whereas a space is not. Hence the method will correctly escape a space, but leaves + and & intact.
Reading RFC2396 http://www.ietf.org/rfc/rfc2396.txt - there is a set of reserved and unreserved characters defined. My guess is that none of these characters are escaped by stringByAddingPercentEscapesUsingEncoding.