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

最新文章

  1. CBitmap、HBITMAP、BITMAP相互转换
  2. yii2 pjax使用
  3. 纯脚本组装Json格式字符串
  4. android sudio 打包资料汇总
  5. 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常的解决办法
  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计
  7. GitHub上传不了的解决 ssh: connect to host github.com port 22: Bad file number git did not exit cleanly (exit code 128)
  8. 让Linux开机运行命令
  9. Drupal 7 电子邮件的发送设置 SMTP, Mail System, Mime Mail
  10. 浅谈DevExpress&lt;三&gt;:在GridView中加载动态图片
  11. nuget pack 时不包含依赖包(而不是引用项目的dll,区别于IncludeReferencedProjects)
  12. Java - 在WebService中使用Client调用三方的RestAPI
  13. C#核心语法讲解-泛型(详细讲解泛型方法、泛型类、泛型接口、泛型约束,了解协变逆变)
  14. 学习STM32F769DK-OTA例程之APP中断向量表重映射
  15. Python数据挖掘(爬虫强化)
  16. 分布式_zookeeper
  17. selenium-确认进入了预期页面(四)
  18. CSS文字过多显示省略号
  19. 解决oracle语句中 含数字的字符串按数字排序问题
  20. Jetty使用内存过大的解决方案

热门文章

  1. 使用Cloudera部署,管理Hadoop集群
  2. CSS网页中的相对定位与绝对定位
  3. 开源app之MyHearts
  4. show table status
  5. linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
  6. kubernetes集群部署
  7. python数据类型之 set
  8. SpringMVC+Apache Shiro+JPA(hibernate)
  9. 百度ueditor学习使用
  10. MySQL在一台db服务器上面如何启动多个实例 (转)