Iphone StringByAddingPercentEscapes不使用符号、问号等
我正在从我的iphone应用程序发送一个请求,其中一些参数是用户可以在文本框中输入的文本。因此,我需要对它们进行HTML编码 以下是我遇到的问题:Iphone StringByAddingPercentEscapes不使用符号、问号等,iphone,objective-c,nsurl,Iphone,Objective C,Nsurl,我正在从我的iphone应用程序发送一个请求,其中一些参数是用户可以在文本框中输入的文本。因此,我需要对它们进行HTML编码 以下是我遇到的问题: NSLog(@"%@", testText); // Test & ? testText = [testText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@", testText); // Test%20&%20? 正如您所看
NSLog(@"%@", testText); // Test & ?
testText = [testText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", testText); // Test%20&%20?
正如您所看到的,只有空格被编码,这使得服务器忽略了参数中除符号和之外的所有内容
这是StringByAddingPercentEscape的广告行为吗?我必须用相应的十六进制代码手动替换每个特殊字符吗
感谢您的贡献。它们没有编码,因为它们是有效的URL字符。
stringbyaddingpercentescapesusingencode:
的文档说明
有关更复杂的转换,请参见CFURLCreateStringByAddingPercentEscapes
我使用以下方法对查询字符串参数进行编码(添加到NSString
类别)
它们不是编码的,因为它们是有效的URL字符。
stringbyaddingpercentescapesusingencode:
的文档说明
有关更复杂的转换,请参见CFURLCreateStringByAddingPercentEscapes
我使用以下方法对查询字符串参数进行编码(添加到NSString
类别)
- (NSString *)urlEncodedString {
CFStringRef buffer = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
NSString *result = [NSString stringWithString:(NSString *)buffer];
CFRelease(buffer);
return result;
}