iOS HTTP请求 URL中含有特殊字符处理

iOS HTTP请求 URL中含有特殊字符  !$&'()*+,;=  处理

有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了。

解决办法:

对参数做编码,代码如下:

-(NSString*)encodeUrlParam

{

    static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4

    staticNSString*constkAFCharactersSubDelimitersToEncode =@"!$&'()*+,;=";


    NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];

    [allowedCharacterSetremoveCharactersInString:[kAFCharactersGeneralDelimitersToEncodestringByAppendingString:kAFCharactersSubDelimitersToEncode]];


    // FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028

    // return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];


    staticNSUIntegerconstbatchSize =50;


    NSUIntegerindex =0;

    NSMutableString*escaped =@"".mutableCopy;


    while(index

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored"-Wgnu"

        NSUIntegerlength =MIN(self.length - index, batchSize);

#pragma GCC diagnostic pop

        NSRangerange =NSMakeRange(index, length);

       range = [self rangeOfComposedCharacterSequencesForRange:range];


        NSString*substring = [selfsubstringWithRange:range];

        NSString*encoded = [substringstringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];

        [escapedappendString:encoded];


        index += range.length;

    }


    returnescaped;

}


你可能感兴趣的:(iOS HTTP请求 URL中含有特殊字符处理)