URL編碼CFURLCreateStringByAddingPercentEscapes使用(ARC)

來源:互聯網
上載者:User

 URL 編碼:CFURLCreateStringByAddingPercentEscapes If you have tried to send any information using a GET web request, you would have come cross an annoying problem, That annoying problem is making sure that the URL is corrently 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.

   也就是說,你提供的 URL 字串 裡面可能包含某些字元,比如‘$‘ ‘&’ ‘?’...等,這些字元在 URL 文法中是具有特殊文法含義的,

比如 URL :http://www.baidu.com/s?wd=%BD%AA%C3%C8%D1%BF&rsv_bp=0&rsv_spt=3&inputT=3512 

中 的 & 起到分割作用 等等,如果 你提供的URL 本身就含有 這些字元,就需要把這些字元 轉化為 “%+ASCII” 形式,以免造成衝突。

  這就引入:CFURLCreateStringByAddingPercentEscapes 函數。

  該函數將 將要添加到URL的字串進行特殊處理,如果這些字串含有 &, ? 這些特殊字元,用“%+ASCII” 代替之。

 

CFURLCreateStringByAddingPercentEscapes(   kCFAllocatorDefault,   (CFStringRef)parameter,  NULL,  

CFSTR(":/?#[]@!$&’()*+,;="),   kCFStringEncodingUTF8  );  

// 確定 parameter 字串中含有:/?#[]@!$&’()*+,;= 這些字元時候,這些字元需要被轉化,以免與文法衝突,其中空格是預設被轉化的,所以沒有列出來    

 


例如: 建立一個 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 = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)[parameters valueForKey:key],nil,CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),   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("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),   kCFStringEncodingUTF8);

      escapedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
      [urlString appendFormat:@"&%@=%@",key,escapedString]; 
    }
keyIndex++;

}
     return [NSURL URLWithString:urlString];
}

@end

調用測試:

    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);

輸出:

the url : http://twitter.com/statuses/update.xml?status=This%20is%20my%20status&meyers=meng%20ya

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.