URL encoding cfurlcreatestringbyaddingpercentescapes (ARC)

Source: Internet
Author: User
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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.