ios - NSURLComponents 获取百分比编码

标签 ios nsurl foundation percent-encoding

根据 NSURLComponents 的文档:

If you set the unencoded property, you can then obtain the encoded equivalent by reading the encoded property value and vice versa.

我试图获取查询的百分比编码版本和 url 的路径,但我错过了一些东西:

NSURL * url = [NSURL URLWithString:@"http://google.com"];

NSURLComponents * components = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:YES];
components.query = @"oauth_callback=http://google.com";

NSLog(@"Encoded: %@", [components URL]); 
//-> Encoded: http://google.com?oauth_callback=http://google.com
//Does not encode "/" or ":"

我在哪里失败了?

最佳答案

我认为 NSURLComponent 的行为是正确的。 来自 Wikipedia :

In the "query" component of a URI (the part after a ? character), for example, "/" is still considered a reserved character but it normally has no reserved purpose, unless a particular URI scheme says otherwise. The character does not need to be percent-encoded when it has no reserved purpose.

关于ios - NSURLComponents 获取百分比编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259961/

相关文章:

ios - 在 Swift 中创建数组时的一般问题

ios - 防止调用静态库中的函数

ios - 银联银行 : Prevent backup files on iCloud

ios - NSURLRequestReloadIgnoringLocalAndRemoteCacheData 在 iOS 7 中工作吗?

ios - 如何防止修改字典副本

ios - 在 Xcode 中,如何抑制菜单添加的 "MARK:"部分?

android - Web 应用程序可以控制 native 操作系统控件吗?

ios - 尝试重命名 Documents 目录中的文件时出现错误 "file:/..."

swift - 将加号 (+) 解码为 URLComponent 中的空格

c++ - 我想在我的 objective -c 项目中读取 .bin 文件并相应地解析数据