iOS url 不支持问题

RFC 1738 硬性规定:

只有字母和数字[0-9a-zA-Z]、一些特殊符号"$-_.+!*’(),"[不包括双引号]、以及某些保留字,才可以不经过编码直接用于URL。
如下链接:

http://www.xxx.cn/How do you spell Mendes.pdf

如上,url 中存在空格,我们必须编码后使用,这样服务端才能理解客户端的请求。否则出现error.

解决办法:
NSString *urlString = @"http://www.HAN.cn/How do you spell Mendes.pdf";
    NSCharacterSet *urlStringSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *handleString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:urlStringSet];
    NSLog(@"handleString =%@", handleString);

输出结果:

handleString =http://www.HAN.cn/How%20do%20you%20spell%20Mendes.pdf

推荐文章:
关于URL编码

猜你喜欢

转载自blog.csdn.net/weixin_41463971/article/details/86571472