iOS URL转码

最近开发一个相册的功能,测试人员反馈部分图片加载不出来,但是安卓却可以正常显示。第一反应应该就是图片的URL有问题,于是断点调试将图片的URL打印出来,果不其然发现图片URL里面含有中文。于是在网上搜索了一下,发现有两种解决方案,下面将这两种方法对比一下。

//图片url
http://10.10.10.10:8080/file/download?token=xxx&path=/home/47825309/素材库/(壁纸)素材库更新50000张壁纸/1280及以下高清壁纸/7_4c14478879c1b.jpg&type=1

一、方法一 :逐个字符判断转码

//处理特殊字符
- (NSString *)isChinese:(NSString *)str {
   NSString *newString = str;
   //遍历字符串中的字符
   for(int i=0; i< [str length];i++){
       int a = [str characterAtIndex:i];
       //汉字的处理
       if( a > 0x4e00 && a < 0x9fff)
       {
           NSString *oldString = [str substringWithRange:NSMakeRange(i, 1)];
           NSString *string = [oldString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
           newString = [newString stringByReplacingOccurrencesOfString:oldString withString:string];
       }
       //空格处理
       if ([newString containsString:@" "]) {
           newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
       }
   }
   return newString;
}

转码后的结果:

http://10.10.10.10:8080/file/download?token=xxx&path=/home/47825309/%E7%B4%A0%E6%9D%90%E5%BA%93/(%E5%A3%81%E7%BA%B8)%E7%B4%A0%E6%9D%90%E5%BA%93%E6%9B%B4%E6%96%B050000%E5%BC%A0%E5%A3%81%E7%BA%B8/1280%E5%8F%8A%E4%BB%A5%E4%B8%8B%E9%AB%98%E6%B8%85%E5%A3%81%E7%BA%B8/7_4c14478879c1b.jpg&type=1

这种方法只会对url中的中文进行转码,如果url里含有特殊符号比如括号,就不会转码。所以如果url中含有特殊符号,这种方法就不适用了。

二、方法二 :统一转码

NSString *testUlr13 = [str stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^%*+,:;'\"`<>()[]{}/\\| "] invertedSet]];

转码后的结果:

http%3A%2F%2F10.10.10.10%3A8080%2Ffile%2Fdownload%3Ftoken=xxx&path=%2Fhome%2F47825309%2F%E7%B4%A0%E6%9D%90%E5%BA%93%2F%EF%BC%88%E5%A3%81%E7%BA%B8%EF%BC%89%E7%B4%A0%E6%9D%90%E5%BA%93%E6%9B%B4%E6%96%B050000%E5%BC%A0%E5%A3%81%E7%BA%B8%2F1280%E5%8F%8A%E4%BB%A5%E4%B8%8B%E9%AB%98%E6%B8%85%E5%A3%81%E7%BA%B8%2F7_4c14478879c1b.jpg&type=1

这种方式将url中所有的字符都转码了,包括http的冒号和双斜杠,这种方式肯定不可取。很明显我们只需要对后面的参数进行转码,改进后的方法:

//处理特殊字符
- (NSString *)isSpecialStr:(NSString *)str {
   NSString *newString = str;
    if ([newString containsString:@"&path="]) {
        NSArray *paths = [newString componentsSeparatedByString:@"&path="];
        NSString *pathString1 = paths.firstObject;
        NSString *pathString2 = paths.lastObject;
        pathString2 = [pathString2 stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]];
        newString = [NSString stringWithFormat:@"%@&path=%@",pathString1,pathString2];
    }
    NSLog(@"路径:%@", newString);
   return newString;
}

转码后的结果:

http://10.10.10.10:8080/file/download?token=xxx&path=%2Fhome%2F47825309%2F%E7%B4%A0%E6%9D%90%E5%BA%93%2F%EF%BC%88%E5%A3%81%E7%BA%B8%EF%BC%89%E7%B4%A0%E6%9D%90%E5%BA%93%E6%9B%B4%E6%96%B050000%E5%BC%A0%E5%A3%81%E7%BA%B8%2F1280%E5%8F%8A%E4%BB%A5%E4%B8%8B%E9%AB%98%E6%B8%85%E5%A3%81%E7%BA%B8%2F7_4c14478879c1b.jpg&type=1

本来以为这样就解决了,但是图片还是加载不了,于是让安卓的同学将这个url转码之后,结果是这样的:

http://10.10.10.10:8080/file/download?token=xxx&path=%2Fhome%2F47825309%2F%E7%B4%A0%E6%9D%90%E5%BA%93%2F%EF%BC%88%E5%A3%81%E7%BA%B8%EF%BC%89%E7%B4%A0%E6%9D%90%E5%BA%93%E6%9B%B4%E6%96%B050000%E5%BC%A0%E5%A3%81%E7%BA%B8%2F1280%E5%8F%8A%E4%BB%A5%E4%B8%8B%E9%AB%98%E6%B8%85%E5%A3%81%E7%BA%B8%2F7_4c14478879c1b.jpg%26type%3D1

经过对比发现,安卓的转码中,没有对 & 和 = 这两个特殊字符进行转码。

删除这两个特殊字符,就和安卓的转码一致了:

//处理特殊字符
- (NSString *)isSpecialStr:(NSString *)str {
   NSString *newString = str;
    if ([newString containsString:@"&path="]) {
        NSArray *paths = [newString componentsSeparatedByString:@"&path="];
        NSString *pathString1 = paths.firstObject;
        NSString *pathString2 = paths.lastObject;
       pathString2 = [pathString2 stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^%*+,:;'\"`<>()[]{}/\\| "] invertedSet]]; //去掉了 & 和 = 这两个字符串
        newString = [NSString stringWithFormat:@"%@&path=%@",pathString1,pathString2];
    }
    NSLog(@"路径:%@", newString);
   return newString;
}

三、NSCharacterSet对象

@interface NSCharacterSet (NSURLUtilities)
// 预定义字符集用于六个URL组件和子组件,它们允许百分比编码。 
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters

// 包含URL用户子组件中允许的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLUserAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL密码子组件中允许的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLPasswordAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL的主机子组件中允许的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLHostAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 返回一个包含字符的字符集允许URL的路径组件。字符“;”是一种合法的路径,但是建议最好是percent-encoded兼容NSURL(-stringByAddingPercentEncodingWithAllowedCharacters:percent-encode任何‘;’字符如果你通过URLPathAllowedCharacterSet)
@property (class, readonly, copy) NSCharacterSet *URLPathAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL查询组件中允许的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLQueryAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL片段组件中允许的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLFragmentAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

@end

编码字符范围
URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`

四、URL不需要编码的字符

HTTP URL 使用的RFC3986编码规范,RFC3986文档规定,URL中只允许包含以下四种:

1、英文字母(a-z A-Z)

2、数字(0-9)

3、-_.~ 4个特殊字符

4、所有保留字符,RFC3986中指定了以下字符为保留字符(英文字符): ! * ' ( ) ; : @ & = + $ , / ? # [ ]

5、编码标记符号 %

猜你喜欢

转载自blog.csdn.net/u010545480/article/details/128728657