URL 编码:CFURLCreateStringByAddingPercentEscapes
来源:互联网 发布:淘宝首页图片不显示 编辑:程序博客网 时间:2024/05/21 00:55
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(":/?#[]@!$&’()*+,;="), // 确定 parameter 字符串中含有:/?#[]@!$&’()*+,;=这些字符时候,这些字符需要被转化,以免与语法冲突。 kCFStringEncodingUTF8);
例如: 建立一个 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) { escapedString =(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[parameters valueForKey:key], NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8); [urlString appendFormat:@"?%@=%@",key,escapedString]; [escapedString release]; }else{ escapedString =(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[parameters valueForKey:key], NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8); [urlString appendFormat:@"&%@=%@",key,escapedString]; [escapedString release]; } 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
另外就是 CFURLCreateStringByAddingPercentEscapes 将自动将待转化的字符串中的空格 转化为 : %20, 即 空格字符的 ASCII,
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes[转]
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- IOS开发之CFURLCreateStringByAddingPercentEscapes--URL 编码
- IOS开发之CFURLCreateStringByAddingPercentEscapes--URL 编码
- IOS使用CFURLCreateStringByAddingPercentEscapes进行URL编码
- IOS使用CFURLCreateStringByAddingPercentEscapes进行URL编码
- IOS使用CFURLCreateStringByAddingPercentEscapes进行URL编码
- URL Encoding - CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- CFURLCreateStringByAddingPercentEscapes
- url编码
- Cocos2d-x制作太空射击游戏
- 移动2D游戏框架 Cocos2d-x 学习笔记
- 【iOS-cocos2d-X 游戏开发之二】【必看篇】总结阐述Cocos2d-X与Cocos2d-iphone区别
- BOOST静态编译
- Fun shadow effects using custom CALayer shadowPaths
- URL 编码:CFURLCreateStringByAddingPercentEscapes
- 【Cocos2d-X(1.x 2.x) 修复篇】iOS6 中 libcurl.a 无法通过armv7s编译以及iOS6中无法正常游戏横屏的解决方法
- [Java教程]将Java Application转换成.exe可执行文件
- android fragment(android.support.v4.app.Fragment和android.app.Fragment区别)
- LayoutInflater的inflate函数
- open和fopen的区别联系
- android xml-----Sax解析
- 第一周 项目一 月份季节的转换
- UVa 127--``Accordian'' Patience