23

我们如何在 URL 中传递字符串 Mr.X & Mr.Y。

我已经尝试过了,但是这个可以处理所有字符,但可以使用 & 符号。

[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
4

6 回答 6

36

甚至更短:

@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {            
    return [(NSString *) CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (CFStringRef)[[self mutableCopy] autorelease], 
        NULL, 
        CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),
        kCFStringEncodingUTF8) autorelease];
}
@end

在这里,它再次作为符合 ARC 的内联函数助手:

#if __has_feature(objc_arc)
static inline NSString *hxURLEscape(NSString *v) {
    static CFStringRef _hxURLEscapeChars = CFSTR("=,!$&'()*+;@?\r\n\"<>#\t :/");
    return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
        NULL, 
        (__bridge CFStringRef)[v mutableCopy], 
        NULL, 
        _hxURLEscapeChars, 
        kCFStringEncodingUTF8));
}
#endif
于 2009-07-28T08:11:44.633 回答
32

-stringByAddingPercentEscapesUsingEncoding:不执行完整的转义编码。您应该使用手动添加编码-replaceOccurrencesOfString:withString:

这是最初在https://devforums.apple.com/message/15674#15674建议的完整列表(反映 Gamecat 的列表)。正如尼克指出的那样,这很昂贵,所以不要简单地包含完整列表而不考虑您对逃生的真正要求。

NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];   
[escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];

此代码肯定会对您的 URL 进行哈希处理。

于 2009-04-01T15:28:54.813 回答
4

使用 %26 作为 url 转义。

其他逃生:

$  %24
&  %26
+  %2B
,  %2C
/  %2F
:  %3A
;  %3B
=  %3D
?  %3F
@  %40
于 2009-04-01T13:30:04.630 回答
0

stringByAddingPercentEscapesUsingEncoding也不能正常工作+

这是一个更简单的解决方案:

[[[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"&" withString:@"%26"] stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
于 2013-05-12T23:24:00.943 回答
0

CFURLCreateStringByAddingPercentEscapes已弃用(自 iOS9 起),那么仅修改 URLQueryAllowedCharacterSet 以删除保留字符如何,从而允许相对有效地对它们进行百分比编码?

- (NSString *)URLQueryValueEncodedString:(NSString *)string {
    static NSMutableCharacterSet *_allowedCharacterSet = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *reservedCharacters = @"=,!$&'()*+;@?\r\n\"<>#\t :/[]%";
        _allowedCharacterSet = [NSCharacterSet URLQueryAllowedCharacterSet].mutableCopy;
        [_allowedCharacterSet removeCharactersInString:reservedCharacters];
    });

    return [string stringByAddingPercentEncodingWithAllowedCharacters:_allowedCharacterSet];
}
于 2018-08-03T06:19:34.103 回答
-2

在 URL 中,& 符号是一个受保护的关键字,表示包含查询字符串变量。你不能把它作为价值本身的一部分。您需要将其更改为其他内容。

这是 StackOverflow 上提出的相同问题的链接:ASP.Net URLEncode Ampersand for use in Query String

于 2009-04-01T13:24:34.033 回答