iOS URL转码
admin
2024-05-13 23:27:34

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

相关内容

热门资讯

宝鸡擀面皮的“出圈”之路 在关中平原的厚土之上,风与麦浪年复一年地轻声私语。这温柔的互动,孕育出一种独具特色、醇厚浓郁的地道滋...
当“代谢效率”成为社交硬通货,... 过去,我曾以为“品牌”是白酒唯一的护城河,后来觉得“窖池”才是;但到了2026年,当我和新一代的商务...
闲聊吃喝 榆次南炒面 闲聊吃喝 榆次南炒面 榆次,古称魏榆,紧傍省城,为南北东西交通要道,晋商汇聚之地,饮食具有北国特色,...
家常清炒胡萝卜,简单三步惊艳味... 厨房的窗户透进午后偏斜的光,照在砧板上一根饱满的橙红色胡萝卜上,表皮光滑紧实,还沾着些许泥土的清新气...
为尝这口非遗软糕,我专程驱车去... 上海松江叶榭镇的老街,在微雨的清晨总泛着一层青灰色的光。湿漉漉的石板路上,脚步声稀疏,空气里却早已飘...