On Saturday, December 15, 2001, at 08:42 PM, Circlaeys Eric wrote:
> Just a question for you,
> I am looking for using NSURL to send a request to an apache server.
> But the problem is I cannot build a NSURL with a string like this :
> "http://toto.fr:80/script.php?v=value@/^777"
>
> How can I do to encode value@/^777 by %xx as urlEncode done !
I didn't find anything in Foundation for this. It would be a useful
addition.
This should work:
@implementation NSString (URLEncoding)
static NSArray *urlCodes;
- (NSArray *)urlCodes
{
// See:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
if (!urlCodes)
{
NSMutableArray *c = [NSMutableArray array];
urlCodes = c;
[c addObject:[NSArray arrayWithObjects:@"%", @"%25", nil]];
[c addObject:[NSArray arrayWithObjects:@" ", @"%20", nil]];
[c addObject:[NSArray arrayWithObjects:@"/", @"%22", nil]];
[c addObject:[NSArray arrayWithObjects:@" ", @"%20", nil]];
[c addObject:[NSArray arrayWithObjects:@">", @"%3E", nil]];
[c addObject:[NSArray arrayWithObjects:@"}", @"%7D", nil]];
[c addObject:[NSArray arrayWithObjects:@"\\", @"%5C", nil]];
[c addObject:[NSArray arrayWithObjects:@"~", @"%7E", nil]];
[c addObject:[NSArray arrayWithObjects:@"]", @"%5D", nil]];
[c addObject:[NSArray arrayWithObjects:@"[", @"%5B", nil]];
[c addObject:[NSArray arrayWithObjects:@";", @"%3B", nil]];
[c addObject:[NSArray arrayWithObjects:@"?", @"%3F", nil]];
[c addObject:[NSArray arrayWithObjects:@"@", @"%40", nil]];
[c addObject:[NSArray arrayWithObjects:@"&", @"%26", nil]];
[c addObject:[NSArray arrayWithObjects:@"<", @"%3C", nil]];
[c addObject:[NSArray arrayWithObjects:@"#", @"%23", nil]];
[c addObject:[NSArray arrayWithObjects:@"{", @"%7B", nil]];
[c addObject:[NSArray arrayWithObjects:@"}", @"%7D", nil]];
[c addObject:[NSArray arrayWithObjects:@"|", @"%7C", nil]];
[c addObject:[NSArray arrayWithObjects:@"^", @"%5E", nil]];
[c addObject:[NSArray arrayWithObjects:@"`", @"%60", nil]];
[c addObject:[NSArray arrayWithObjects:@"/", @"%2F", nil]];
[c addObject:[NSArray arrayWithObjects:@":", @"%3A", nil]];
[c addObject:[NSArray arrayWithObjects:@"=", @"%3D", nil]];
[c addObject:[NSArray arrayWithObjects:@"$", @"%24", nil]];
}
return urlCodes;
}
- (NSString *)urlEncoded
{
NSString *s = self;
NSArray *codes = [self urlCodes];
NSEnumerator *enm = [codes objectEnumerator];
NSArray *code;
while (code = [enm nextObject])
{
NSString *s1 = [code objectAtIndex:0];
NSString *s2 = [code objectAtIndex:1];
s = [s stringByReplacingSubstring:s1 withString:s2];
}
return s;
}
- (NSString *)urlDecoded
{
NSString *s = self;
NSArray *codes = [self urlCodes];
NSEnumerator *enm = [codes objectEnumerator];
NSArray *code;
while (code = [enm nextObject])
{
NSString *s1 = [code objectAtIndex:1];
NSString *s2 = [code objectAtIndex:0];
s = [s stringByReplacingSubstring:s1 withString:s2];
}
return s;
}
- (NSString *)stringByReplacingSubstring:(NSString *)substring
withString:(NSString *)replaceString
{
NSMutableString *string = [NSMutableString stringWithString:self];
NSRange range;
if ( !replaceString || !substring ) { return self; }
range = [string rangeOfString:substring];
while ( range.length )
{
[string replaceCharactersInRange:range withString:replaceString];
//range.location = range.location + range.length;
range.location = range.location + 1;
range.length = [string length] - range.location;
range = [string rangeOfString:substring options:0 range:range];
}
return string;
}
@end