将 NSString 转换为可在 HTTP POST 请求中传递的形式
有没有一种简单的方法来转义 NSString,以便我可以在 HTTP POST 请求中使用它?
我尝试过 stringByAddingPercentEscapesUsingEncoding
,但以下:
[@"~!@#$%^&*()-+\"'" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
给了我:
~!@%23$%25%5E&*()-+%22'
在 HTTP POST 查询中不可用,因为 &
未转义;因此,另一端将其后面的任何内容解释为另一个参数。
Is there a simple way to escape a NSString so I can use it in a HTTP POST request?
I've tried stringByAddingPercentEscapesUsingEncoding
, but the following:
[@"~!@#$%^&*()-+\"'" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
gives me:
~!@%23$%25%5E&*()-+%22'
which is not usable in a HTTP POST query because the &
is left unescaped; therefore the other end interprets whatever follows it as another argument.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下函数是
NSString+URLEncoding.m
的一部分,完全满足我的需要:The following function, part of
NSString+URLEncoding.m
, does exactly what I need: