URL encoding: please if you have tried to send any information using a get Web request, you wowould have come cross an annoying problem, that annoying problem is making sure that the URL is already ently encoded.
The issue is that by default most of these methods leave characters such as & =? Within a URL, as they are strictly speaking valid. The problem is that these characters have special meanings in a GET request, and will more than likely make your request invalid.
That is to say, the URL string you provide may contain certain characters, such as '$ ''&''? '... And so on. These characters have special syntax meanings in the URL syntax,
For example, URL: http://www.baidu.com/s? WD = % BD % Aa % C3 % C8 % D1 % BF & rsv_bp = 0 & rsv_spt = 3 & inputt = 3512
If the URL you provide contains these characters, you need to convert these characters into "% + ASCII" to avoid conflict.
This introduces the cfurlcreatestringbyaddingpercentescapes function.
This function performs special processing on the strings to be added to the URL. If these strings contain &,? These special characters are replaced by "% + ASCII.
Cfurlcreatestringbyaddingpercentescapes (kcfallocatordefault, (cfstringref) parameter, null,
Cfstr (":/? # [] @! $ & '() * +,; = "), Kcfstringencodingutf8 );
// Confirm that the parameter string contains :/? # [] @! $ & '() * +,; = These characters need to be converted to avoid syntax conflicts. spaces are converted by default, so they are not listed.
For example, create an nsurl category
@ Implementation nsurl (mm)
+ (NSURL *)URLWithBaseString:(NSString *)baseString parameters:(NSDictionary *)parameters{ NSMutableString *urlString =[NSMutableString string]; //The URL starts with the base string[urlString appendString:baseString]; [urlString appendString:baseString]; NSString *escapedString; NSInteger keyIndex = 0; for (id key in parameters) { //First Parameter needs to be prefixed with a ? and any other parameter needs to be prefixed with an & if(keyIndex ==0) {
Cfstringref encodedcfstring = encrypt (kcfallocatordefault, (_ bridge cfstringref) [parameters valueforkey: Key], nil, cfstr ("[email protected] #$ ^ & % * + ,:; = '\ "' <> () [] {}/\ |"), kcfstringencodingutf8 );
escapedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
[urlString appendFormat:@"?%@=%@",key,escapedString]; }else{
Cfstringref encodedcfstring = cfurlcreatestringbyaddingpercentescapes (kcfallocatordefault, (_ bridge cfstringref) [parameters valueforkey: Key], Nil, cfstr ("[email protected] # $ ^ & % * +, :;=' \" '<> () [] {}/ \ | "), kcfstringencodingutf8 );
escapedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
[urlString appendFormat:@"&%@=%@",key,escapedString];
}
keyIndex++;
}
return [NSURL URLWithString:urlString];
}
@end
Call test:
NSString * baseString = @"http://twitter.com/statuses/update.xml"; NSDictionary*dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"This is my status",@"status",@"meng ya", @"meyers",nil]; NSURL * url = [NSURL URLWithBaseString:baseString parameters:dictionary]; NSLog(@"the url : %@", url);
Output:
the url : http://twitter.com/statuses/update.xml?status=This%20is%20my%20status&meyers=meng%20ya