AFNetworking: Encoding URL strings containing the% sign -

I am using AFNetworking in my iOS application. I found a problem that occurs when query parameters contain percent signs. Then encoding is not performed. The AFURLEncodedStringFromStringWithEncoding method returns nil.

I tested this code and it prints (null) :

 NSLog(@"%@", AFURLEncodedStringFromStringWithEncoding(@"%", NSUTF8StringEncoding)); 

Expected result should be: %25 . Other characters can be encoded without problems.

The method is defined as follows:

 NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEncoding encoding) { static NSString * const kAFLegalCharactersToBeEscaped = @" ?!@ #$^&%*+,:;='\"`<>()[]{}/\\|~ "; // Following the suggestion in documentation for `CFURLCreateStringByAddingPercentEscapes` to "pre-process" URL strings (using stringByReplacingPercentEscapesUsingEncoding) with unpredictable sequences that may already contain percent escapes. return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[string stringByReplacingPercentEscapesUsingEncoding:encoding], NULL, (CFStringRef)kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease]; } kCFAllocatorDefault, (CFStringRef) [string stringByReplacingPercentEscapesUsingEncoding: encoding], NULL, (CFStringRef) kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding (encoding)) autorelease]; NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEncoding encoding) { static NSString * const kAFLegalCharactersToBeEscaped = @" ?!@ #$^&%*+,:;='\"`<>()[]{}/\\|~ "; // Following the suggestion in documentation for `CFURLCreateStringByAddingPercentEscapes` to "pre-process" URL strings (using stringByReplacingPercentEscapesUsingEncoding) with unpredictable sequences that may already contain percent escapes. return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[string stringByReplacingPercentEscapesUsingEncoding:encoding], NULL, (CFStringRef)kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease]; } 

Any ideas what is going wrong here?

EDIT: Issue resolved in AFNetworking .

+4
source share
2 answers

I don't know about the API, but URL encoding is usually done

  • UTF-8 encoding strings - path, arguments, etc. - to bytes
  • applying percent encoding of byte values ​​to get ASCII string fragments
  • collection of URL fragments with delimiters:, / ,? etc.
  • encoding this string in bytes again
  • sending by cable

See http://tools.ietf.org/html/rfc3986#section-2.5 for nuances. Please note that the RFC states that if the URL refers to a document in the EBCDIC system, the URL should specify the byte values ​​for the EBCDIC encoding of its name, and not the name of the string that users of EBCDIC users know. Thus, the URL is ultimately a byte string, not a character string.

As for how to properly configure your API for the correct byte string, I'm not sure.

+1
source

Just removing stringByReplacingPercentEscapesUsingEncoding:encoding is fine, there is no need to code twice, so the return value should be:

 return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, (CFStringRef)kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease]; 
0
source

Source: https://habr.com/ru/post/1392974/


All Articles