iOS URL转码
admin
2024-05-14 00:19:33
0

最近开发一个相册的功能,测试人员反馈部分图片加载不出来,但是安卓却可以正常显示。第一反应应该就是图片的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、编码标记符号 %

相关内容

热门资讯

好用的看电影App推荐 202... 想随时随地追剧观影,又不想受限于电视时段或影院排片?如今,一部智能手机就能轻松满足你的观影需求。本文...
查理九世免费阅读软件推荐 20... 童年经典再掀热潮!《查理九世》作为一代人的成长印记,承载着无数读者对冒险、友情与成长的最初想象。墨多...
手机解压软件推荐|支持RAR/... 手机解压软件rar,日常使用中,许多用户常遇到从微信、网盘等渠道接收的压缩包无法打开的问题。手机系统...
免费聊天App推荐 不用充钱也... 当人们感到空闲或情绪低落时,常常渴望与他人进行即时、轻松的交流。市面上聊天类应用种类繁多,但如何筛选...
好用的看球软件推荐 免费高清无... 球迷在挑选观赛应用时,核心关注点往往集中在三点:实时比分推送是否及时、覆盖赛事是否全面、直播数据是否...
好用的远程控制软件推荐 远程桌... 提到远程控制软件,很多人首先想到的仍是办公场景。但实际上,这类工具的应用早已延伸至日常生活多个角落:...
中老年人免费交友软件推荐 适合... 中老年人社交需求日益增长,但受限于生活半径和数字技能,拓展新朋友常面临实际困难。本文精选五款专为中老...
解压专家APP推荐 2026年... 高效处理手机端各类压缩文件,选择功能全面、操作便捷的解压工具至关重要。当前安卓平台有哪些值得推荐的专...
办公软件有哪些 好用的办公AP... 在数字化办公日益普及的今天,高效、轻便、功能全面的移动办公应用已成为职场人士不可或缺的生产力工具。面...
免费看漫画软件推荐 免费最全的... 近年来,国产漫画产业蓬勃发展,各类漫画阅读应用层出不穷。然而,不少用户在使用过程中常遇到付费门槛高、...