URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)

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("[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

调用测试:

    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

URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)

时间: 2024-08-27 06:59:21

URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)的相关文章

URL编码

原链接在这里 Base64编码 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上作为垫字的"=",实际上是65个字符)----作为一个基本字符集.然后,其他所有符号都转换成这个字符集中的字符. 第一步,将每三个字节作为一组,一共是24个二进制位. 第二步,将这24个二进制位分为四组,每个组有6个二进制位. 第三步,在每组前面加两个00,扩展成32个二进制位,即四个字节. 第四步,

[iOS]通过JS调用iOS函数时的URL编码问题

在前面的文章:[iOS]在WebApp中如何使用JS调用iOS的函数 中,提到了如何使用JS通过修改URL调用iOS的内部函数. 其中会遇到一个问题,就是编码问题,比如通过URL调用弹窗,在里面写上内容:你好汪海. 那链接大概就是这样的:http://xxx.com#ios?action=alert&param=你好汪海 但是在iOS中接收到的时候会出现中文的乱码: http://xxx.com#ios?action=alert&param=%25E6%2596%2587%25E4 遇到这

Base64编码 - Md5加密 - url编码

#pragma mark --------编码-------------- + (NSString * )encodeBase64:(NSString * )text { NSData * data = [text dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; // 转换到base64 data = [GTMBase64 encodeData:data]; NSString * base64String = [

对URL编码

url支持26个英文字母.数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,就需要对其进行编码.iOS中提供了函数stringByAddingPercentEscapesUsingEncoding对中文和一些特殊字符(下面已证实包含"%")进行编码,但是stringByAddingPercentEscapesUsingEncoding的功能并不完善,对一些较为特殊的字符无效,或者说对一些我不想编码的字符编码,而对这些字符则可以使用CFURLCreateStringBy

iOS下URL编码

使用NSString默认的URL编码,会对汉字等进行转义,但是对=+等这些字符不会进行转义 所以要手动实现这些字符的转义 - (NSString *)encodeToPercentEscapeString: (NSString *) input { // Encode all the reserved characters, per RFC 3986 // (<http://www.ietf.org/rfc/rfc3986.txt>) NSString *outputStr = (NSStri

URL编码与解码

通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义. 例如Url参数字符串中使用key=value键值对这样的形式来传参,键值对之间以&符号分隔,如/s?q=abc&ie=utf-8.如果你的value字符串中包含了=或者&,那么势必会造成接收Url的服务器解析错误,因此必须将引起歧义的&和=符号进行转义,也就是对其进行编码. 又如,Url的编码格式采用的是ASC

url 编码(percentcode 百分号编码)

http://www.imkevinyang.com/2009/08/%E8%AF%A6%E8%A7%A3javascript%E4%B8%AD%E7%9A%84url%E7%BC%96%E8%A7%A3%E7%A0%81.html 摘要 本文主要针对URI编解码的相关问题做了介绍,对Url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript 中和编解码相关的几对函数escape / unescape,encodeURI / decodeURI和encodeUR

[iOS]URL编码和解码

1. 首先来看下什么样的是URL编码(字符串中带有%22 类似这样的) NSString *str = @"http://m.tuniu.com/api/home/data/index/c/%7B%22v%22%3A%227.1.0%22%2C%22ct%22%3A20%2C%22dt%22%3A1%2C%22p%22%3A11210%2C%22cc%22%3A2500%7D/d/%7B%22clientModel%22%3A%22HONOR+H30-L01%22%2C%22width%22%3

了解URL编码的基本概念,在javascript和java程序中使用内置的API进行编码和解码

1.URL编码的基本概念 URL只能使用US-ASCII 字符集来通过因特网进行发送.由于URL常常会包含 ASCII 集合之外的字符,URL必须转换为有效的 ASCII 格式.URL 编码使用 "%" 其后跟随两位的十六进制数来替换非 ASCII 字符.URL 不能包含空格,URL 编码通常使用 + 来替换空格.所谓URL编码,就是将非US-ASCII字符和US-ASCII中的特殊字符,用相应的字符集编码来表示.比如,汉字"你",如果用UTF-8编码,出现在URL