你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> URL合法化

URL合法化

編輯:IOS開發綜合

URL合法化

iOS開發過程中,網絡數據請求中帶有漢字或者特殊符號需要經過編碼處理,使得URL合法化之後才能進行網絡請求。首先先來模擬一下URL中帶有特殊字符的場景,假設需要將一段JSON字符串作為參數拼接HTTP的Get請求中,代碼如下

    NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};

    //NSJSONWritingPrettyPrinted處理完帶有換行符
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];

    //將data轉換成json格式
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //請求路徑,隨便寫的
    NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";

然後就需要對jsonStr進行PercentEscapes方式的編碼。在介紹如何編碼之前,先說下如何校驗你的URL到底是否合法

    NSString *urlStr = @"xxx";

    //使用類方法直接去生成
    NSURL *URL = [NSURL URLWithString:urlStr];

    //若果返回NULL則不合法,反之合法
    NSLog(@"URL:%@",URL);

第一種處理方式:

    - (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
    //還原 iOS2-9
    - (nullable NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc;

使用這兩個方法進行PercentEscapes方式的編碼和解碼。Percent-encoding簡單來說就是使用”%”+”數字或者字母”來代表URL中的保留字符(就行編程語言的保留字符),例如%20代表空格。示例代碼如下:

    //模擬數據
    NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};

    //NSJSONWritingPrettyPrinted處理完帶有換行符
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];

    //將data轉換成json格式
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //請求路徑,隨便寫的
    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);    輸出結果:

    非法:(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 );

關於參數介紹
allocator:傳NULL 或者 kCFAllocatorDefault 來使用當前默認的 allocator來生成CFSting
originalString:待處理的url字符串
charactersToLeaveUnescaped:需要編碼的保留字符,傳NULL表示所有的保留字符都需要編碼
legalURLCharactersToBeEscaped:需要編碼的合法字符,傳NULL表示沒有合法字符需要編碼
encoding:編碼方式,如果不確定就是用UTF-8 (kCFStringEncodingUTF8)

注意:1、使用范圍iOS2-iOS9
2、官方建議allocator、charactersToLeaveUnescaped、legalURLCharactersToBeEscaped均傳遞NULL來簡化整個編碼過程
3、更多詳情請在Xcode幫助文檔中搜索該方法。

示例代碼如下

    NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};

    //NSJSONWritingPrettyPrinted處理完帶有換行符
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];

    //將data轉換成json格式
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //請求路徑,隨便寫的
    NSString *baseUrl = @"http://192.168.1.1:0001/getdata?";

    //路徑拼接
    NSString *urlStr_cf = [NSString stringWithFormat:@"%@%@",baseUrl,jsonStr];


    //字符轉換
    CFStringRef originalURLString = (__bridge CFStringRef)urlStr_cf;

    /**
        官方建議進行的一步操作,原因:URL不是你預期的那樣或者你不能夠指定charactersToLeaveUnescaped(保留字符)
        就上邊的數據,不進行這一步處理,會默認去掉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"}

小結:第二種方法使用起來比第一張略微麻煩,但是可以指定需要編碼過濾的保留字符,靈活性更好。以上兩種方式均在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);
    關於接口說明:
    1、此兩個方法均是使用UTF-8進行編碼的
    2、allowedCharacters是字符集合,指定的字符集合;也就是說除了指定的字符集之外的都會被編碼替換
    3、此方法是用來針對參數處理的,最好不要整個URL一起編碼

其中allowedCharacters參數是NSCharacterSet類方法可以直接獲取字符集(這裡其實不是集合的概念,便於理解),其中做好了不同類型的字符集合的分類,如下所示

    + (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簡單來說就是包含世界上所有字符的一種編碼方式,如果直觀的查看每個類別下邊到底包含了哪些字符。
此種解決方法的示例代碼如下

    NSDictionary *dataDic = @{@"name":@"zwq",@"sex":@"1",@"num":@"123456789"};

    //NSJSONWritingPrettyPrinted處理完帶有換行符
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:kNilOptions error:nil];

    //將data轉換成json格式
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //請求路徑,隨便寫的
    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);

    輸出結果
    非法:(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"}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved