博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
URL乱码问题
阅读量:5008 次
发布时间:2019-06-12

本文共 3644 字,大约阅读时间需要 12 分钟。

01.
// 将URL编码
02.
- (NSString *)encodeToPercentEscapeString: (NSString *) input
03.
{
04.
NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
05.
(CFStringRef)input,
06.
NULL,
07.
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
08.
kCFStringEncodingUTF8));
09.
return outputStr;
10.
}
11.
 
12.
// 将URL解码
13.
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
14.
{
15.
NSMutableString *outputStr = [NSMutableString stringWithString:input];
16.
[outputStr replaceOccurrencesOfString:@"+"
17.
withString:@" "
18.
options:NSLiteralSearch
19.
range:NSMakeRange(0, [outputStr length])];
20.
 
21.
return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
22.
}

 

 

演示一下上面的封装函数:

 

01.
NSString * testUrl = @";a href="" target="_blank" class="keylink">word</a>s=($# it's {a*123})00!*'();:@&=+$,/?%#[]";
02.
NSLog(@"original: %@", testUrl);
03.
 
04.
 
05.
NSString * encodeStr = [self encodeToPercentEscapeString:testUrl];
06.
NSLog(@"encoded: %@", encodeStr);
07.
 
08.
NSString * encodeStr2 = [testUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
09.
NSLog(@"encoded2:%@", encodeStr2);
10.
 
11.
NSString * decodeStr = [self decodeFromPercentEscapeString:encodeStr];
12.
NSLog(@"decoded: %@", decodeStr);


 

 

结果如下:

>> original: http://search.google.com?keys=($# it's {a*123})00!*'();:@&=+$,/?%#[]

>> encoded: http%3A%2F%2Fsearch.google.com%3Fkeywords%3D%28%24%23%20it%27s%20%7Ba%2A123%7D%2900%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D
>> encoded2: http://search.google.com?keywords=($%23%20it's%20%7Ba*123%7D)00!*'();:@&=+$,/?%25%23%5B%5D
>> decoded: http://search.google.com?keywords=($# it's {a*123})00!*'();:@&=+$,/?%#[]

 

 

 

演示一下自带的URL转码测试代码:

 

01.
NSString* string1 = @"文件夹";
02.
 
03.
NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
04.
 
05.
NSString* string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
06.
 
07.
NSString* string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
08.
 
09.
NSString* string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
10.
 
11.
NSString* string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
12.
 
13.
NSString* string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
14.
 
15.
 
16.
NSLog(@"原始数据%@",string1);
17.
NSLog(@"一层编码%@",string2);
18.
NSLog(@"两层编码%@",string3);
19.
NSLog(@"一层编码的一层解码%@",string4);
20.
NSLog(@"两层编码的一层解码%@",string5);
21.
NSLog(@"一层编码的两层解码%@",string6);
22.
NSLog(@"两层编码的两层解码%@",string7);


 

 

打印结果:

 

2014-06-10 15:00:02.425 DareWayApp[7400:671651] 原始数据https://www.cloudsafe.com/文件夹

2014-06-10 15:00:02.426 DareWayApp[7400:671651] 一层编码https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一层编码的一层解码https://www.cloudsafe.com/文件夹

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码的一层解码https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一层编码的两层解码https://www.cloudsafe.com/文件夹

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 两层编码的两层解码https://www.cloudsafe.com/文件夹

 

如果服务器用的是GBK编码,只要把上面的UTF改成下面的encoding就可以了:

 

 

1.
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
2.
urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:gbkEncoding];

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

转载于:https://www.cnblogs.com/thelatewind/p/4700933.html

你可能感兴趣的文章
Jquery Uploadify3.21.与2.1版本 使用中存在的问题--记录三
查看>>
Linux查看进程的内存占用情况 分类: ubuntu ...
查看>>
[BZOJ 2818]Gcd
查看>>
FORM值传递与地址传递
查看>>
(译)yaml快速教程
查看>>
C:大数相加
查看>>
160. Intersection of Two Linked Lists
查看>>
人生苦短,我用python-- Day11
查看>>
JAVA Bean
查看>>
ehcache memcache redis 三大缓存男高音_转
查看>>
curd_3
查看>>
百度地图API示例之设置地图显示范围
查看>>
Java构造方法、重载及垃圾回收
查看>>
.Net Core AES加密解密
查看>>
Spring Quartz实现任务调度
查看>>
python | 桶排序、冒泡排序、选择排序、去重
查看>>
两个Html页面之间值得传递
查看>>
EasyUI datagrid 的多条件查询
查看>>
Mac升级bash到最新版本
查看>>
利用vagrant打包系统--制作自己的box
查看>>