URL合法化
iOS開發(fā)過程中箱歧,網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求中帶有漢字或者特殊符號(hào)需要經(jīng)過編碼處理呀邢,使得URL合法化之后才能進(jìn)行網(wǎng)絡(luò)請(qǐng)求价淌。首先先來模擬一下URL中帶有特殊字符的場(chǎng)景,假設(shè)需要將一段JSON字符串作為參數(shù)拼接HTTP的Get請(qǐng)求中括尸,代碼如下
NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};
//NSJSONWritingPrettyPrinted處理完帶有換行符
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];
//將data轉(zhuǎn)換成json格式
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//請(qǐng)求路徑濒翻,隨便寫的
NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";
然后就需要對(duì)jsonStr進(jìn)行PercentEscapes方式的編碼肴焊。在介紹如何編碼之前娶眷,先說下如何校驗(yàn)?zāi)愕腢RL到底是否合法
NSString *urlStr = @"xxx";
//使用類方法直接去生成
NSURL *URL = [NSURL URLWithString:urlStr];
//若果返回NULL則不合法,反之合法
NSLog(@"URL:%@",URL);
第一種處理方式:
//編碼 iOS2-9
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
//還原 iOS2-9
- (nullable NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
使用這兩個(gè)方法進(jìn)行PercentEscapes方式的編碼和解碼烁落。Percent-encoding簡(jiǎn)單來說就是使用"%"+"數(shù)字或者字母"來代表URL中的保留字符(就行編程語言的保留字符)豌注,例如%20代表空格轧铁。示例代碼如下:
//模擬數(shù)據(jù)
NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};
//NSJSONWritingPrettyPrinted處理完帶有換行符
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];
//將data轉(zhuǎn)換成json格式
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//請(qǐng)求路徑齿风,隨便寫的
NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";
//非法
NSString *urlStr_illegal = [NSString stringWithFormat:@"%@%@",baseUrl,jsonStr];
NSURL *url_illegal = [NSURL URLWithString:urlStr_illegal];
//合法
NSString *urlStr_legal = [NSString stringWithFormat:@"%@%@",baseUrl,[jsonStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *urlL_legal = [NSURL URLWithString:urlStr_legal];
//還原
NSString *urlStr_restore = [urlStr_illegal stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//非法的NULL
NSLog(@"\n非法:%@\n合法:%@\n還原:%@\n",url_illegal,urlL_legal,urlStr_restore); 輸出結(jié)果:
非法:(null)
合法:http://192.168.1.1:0001/getdata?%7B%22name%22:%22zwq%22,%22sex%22:%221%22,%22num%22:%22123456789%22%7D
還原:http://192.168.1.1:0001/getdata?{"name":"zwq","sex":"1","num":"123456789"}
第二種處理方式:
CFStringRef CFURLCreateStringByAddingPercentEscapes ( CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding );
關(guān)于參數(shù)介紹
allocator:傳NULL 或者 kCFAllocatorDefault 來使用當(dāng)前默認(rèn)的 allocator來生成CFSting
originalString:待處理的url字符串
charactersToLeaveUnescaped:需要編碼的保留字符童本,傳NULL表示所有的保留字符都需要編碼
legalURLCharactersToBeEscaped:需要編碼的合法字符,傳NULL表示沒有合法字符需要編碼
encoding:編碼方式运沦,如果不確定就是用UTF-8 (kCFStringEncodingUTF8)
注意:1、使用范圍iOS2-iOS9
2茶袒、官方建議allocator梯刚、charactersToLeaveUnescaped、legalURLCharactersToBeEscaped均傳遞NULL來簡(jiǎn)化整個(gè)編碼過程
3薪寓、更多詳情請(qǐng)?jiān)赬code幫助文檔中搜索該方法。
示例代碼如下
NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};
//NSJSONWritingPrettyPrinted處理完帶有換行符
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];
//將data轉(zhuǎn)換成json格式
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//請(qǐng)求路徑澜共,隨便寫的
NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";
//路徑拼接
NSString *urlStr_cf = [NSString stringWithFormat:@"%@%@",baseUrl,jsonStr];
//字符轉(zhuǎn)換
CFStringRef originalURLString = (__bridge CFStringRef)urlStr_cf;
/**
官方建議進(jìn)行的一步操作向叉,原因:URL不是你預(yù)期的那樣或者你不能夠指定charactersToLeaveUnescaped(保留字符)
就上邊的數(shù)據(jù),不進(jìn)行這一步處理嗦董,會(huì)默認(rèn)去掉JSON中的換行母谎,否則保留,視情況而定
*/
CFStringRef preprocessedString =
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
//非法
CFURLRef cf_url_illegal = CFURLCreateWithString(kCFAllocatorDefault, originalURLString, NULL);
NSURL *url_illegal = (__bridge NSURL *)cf_url_illegal;
//合法
CFStringRef urlString =
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedString, NULL, NULL, kCFStringEncodingUTF8);
CFURLRef cf_url_legal = CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);
NSURL *url_legal = (__bridge NSURL *)cf_url_legal;
//還原
CFStringRef urlStr_restore =
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, urlString, CFSTR(""), kCFStringEncodingUTF8);
NSLog(@"\n非法:%@\n合法:%@\n還原:%@\n",url_illegal,url_legal,urlStr_restore);
非法:(null)
合法:http://192.168.1.1:0001/getdata?%7B%22name%22:%22zwq%22,%22sex%22:%221%22,%22num%22:%22123456789%22%7D
還原:http://192.168.1.1:0001/getdata?{"name":"zwq","sex":"1","num":"123456789"}
小結(jié):第二種方法使用起來比第一張略微麻煩京革,但是可以指定需要編碼過濾的保留字符奇唤,靈活性更好甲葬。以上兩種方式均在iOS2-iOS9有效,之后即將廢棄,下邊來看下第三種處理方法iOS7之后均可使用配乱。
第三種處理方式:
//編碼
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10_9, 7_0);
//解碼
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding NS_AVAILABLE(10_9, 7_0);
關(guān)于接口說明:
1万栅、此兩個(gè)方法均是使用UTF-8進(jìn)行編碼的
2休溶、allowedCharacters是字符集合徒役,指定的字符集合;也就是說除了指定的字符集之外的都會(huì)被編碼替換
3、此方法是用來針對(duì)參數(shù)處理的坎拐,最好不要整個(gè)URL一起編碼
其中allowedCharacters參數(shù)是NSCharacterSet類方法可以直接獲取字符集(這里其實(shí)不是集合的概念,便于理解)陨晶,其中做好了不同類型的字符集合的分類谆膳,如下所示
+ (NSCharacterSet *)controlCharacterSet; //Unicode General Category Cc and Cf.
+ (NSCharacterSet *)whitespaceCharacterSet; // Unicode General Category Zs and CHARACTER TABULATION (U+0009).
+ (NSCharacterSet *)whitespaceAndNewlineCharacterSet;// Unicode General Category Z*, U+000A ~ U+000D, and U+0085.
+ (NSCharacterSet *)decimalDigitCharacterSet; // Decimal Numbers.
+ (NSCharacterSet *)letterCharacterSet; // Unicode General Category L* & M*.
+ (NSCharacterSet *)lowercaseLetterCharacterSet; //Unicode General Category Ll.
+ (NSCharacterSet *)uppercaseLetterCharacterSet; // Unicode General Category Lu and Lt.
+ (NSCharacterSet *)nonBaseCharacterSet; // Unicode General Category M*.
+ (NSCharacterSet *)alphanumericCharacterSet ; //Unicode General Categories L*, M*, and N*.
+ (NSCharacterSet *)decomposableCharacterSet; //“standard decomposition” in version 3.2 of the Unicode character encoding standard.
+ (NSCharacterSet *)illegalCharacterSet; //Non-Characters or that have not yet been defined in version 3.2 of the Unicode standard.
+ (NSCharacterSet *)punctuationCharacterSet; //Unicode General Category P*.
+ (NSCharacterSet *)capitalizedLetterCharacterSet; //Unicode General Category Lt.
+ (NSCharacterSet *)symbolCharacterSet; //Unicode General Category S*.
+ (NSCharacterSet *)newlineCharacterSet; //newline characters (U+000A ~ U+000D, U+0085, U+2028, and U+2029)
[Unicode General Category](Unicode General Category)中的Unicode簡(jiǎn)單來說就是包含世界上所有字符的一種編碼方式,如果直觀的查看每個(gè)類別下邊到底包含了哪些字符,傳送門在這老客。
此種解決方法的示例代碼如下
NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};
//NSJSONWritingPrettyPrinted處理完帶有換行符
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];
//將data轉(zhuǎn)換成json格式
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//請(qǐng)求路徑苇瓣,隨便寫的
NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";
//非法
NSString *urlStr_illegal = [NSString stringWithFormat:@"%@%@",baseUrl,jsonStr];
NSURL *url_illegal = [NSURL URLWithString:urlStr_illegal];
//合法
NSString *urlStr_legal = [NSString stringWithFormat:@"%@%@",baseUrl,[jsonStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]];
NSURL *urlL_legal = [NSURL URLWithString:urlStr_legal];
//還原
NSString *urlStr_restore = [urlStr_illegal stringByRemovingPercentEncoding];
//非法的NULL
NSLog(@"\n非法:%@\n合法:%@\n還原:%@\n",url_illegal,urlL_legal,urlStr_restore);
輸出結(jié)果
非法:(null)
合法:http://192.168.1.1:0001/getdata?%7B%22name%22%3A%22zwq%22%2C%22sex%22%3A%221%22%2C%22num%22%3A%22123456789%22%7D
還原:http://192.168.1.1:0001/getdata?{"name":"zwq","sex":"1","num":"123456789"}