Question about character of NSString invalid in URL on iPhone

后端 未结 2 1380
逝去的感伤
逝去的感伤 2021-01-13 03:32

Some of my http request has \'space\' in URL, but it is not recognized by iPhone. I have to change \'space\' to \'%20\' whenever there is a \'space\' in the url. Now I have

相关标签:
2条回答
  • 2021-01-13 03:40

    See RFC 2396 for the full gory details. The following characters must be escaped:

    Control characters (ASCII 00-1F and 7F)
    Space
    <
    >
    #
    %
    "
    

    The following characters are unwise to use without escaping because some gateways and other transport agents are known to sometimes modify such characters, or they are used as delimiters:

    { } | \ ^ [ ] `

    0 讨论(0)
  • 2021-01-13 03:43

    I think this is a better approach:

    NSString* escapedUrl = [originalUrl   
    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    If you want to understand how URL encoding works, take a look at this URL.

    0 讨论(0)
提交回复
热议问题