Encode url stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc ?

Discussion in 'iOS Programming' started by cthesky, Nov 26, 2011.

  1. cthesky macrumors member

    Aug 21, 2011
    Hi all,

    I need to get data from web service, one of the data is an URL String. For example: www.testing.aspx?username=123&pwd=456. But after I get this URL String data from web service and used in my Iphone Project, in my Iphone Project this URL String become www.testing.aspx?username=123&pwd=456.

    So, I used a method called - (NSString *)stringByAddingPercentEscapesUsingEncoding: (NSStringEncoding)enc and using NSUTF8StringEncoding. The resulted URL String is : www.testing.aspx?username=123&2pwd=456. The resulted URL string seems not same as what I get from web service.

    Am I doing something wrong?

    Anyone have any idea to encode an URL String? Any idea are appreciated.

    Thanks a lot. :)
  2. jiminaus, Nov 26, 2011
    Last edited: Nov 26, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    Firstly, if you have an percent-escaped URL, you need to use the opposite of what you've tried, which is stringByReplacingPercentEscapesUsingEncoding:.

    But this isn't a percent-escaped URL. The percent-escaped version of & is not & but %26.

    What is &? It's the HTML/XML decimal numeric character reference for &. More commonly the named character reference & is used instead, but & is defined as being &.

    This code will resolve the character references in an HTML/XML text fragment.
    NSXMLNode *node = [[[NSXMLNode alloc] initWithKind:NSXMLTextKind] autorelease];        
    [node setStringValue:@"www.testing.aspx?username=123[plain]&[/plain]pwd=456"
    NSString* url = [node stringValue];

Share This Page