在访问 HTTP 资源时需要对 URL 进行 Encode,从服务器获取资源时,同样需要对文字内容进行Decode,方法如下:
//URLEncode
-(NSString*)encodeString:(NSString*)unencodedString{
// CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
// CharactersToLeaveUnescaped = @"[].";
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"~!*'();@&=+$,%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
//URLDEcode
-(NSString *)decodeString:(NSString*)encodedString
{
NSString *decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(__bridge CFStringRef)encodedString,
CFSTR(""),
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
网友评论