[Cocoa Encoding HTTP]

  • Just a question for you,
    I am looking for using NSURL to send a request to an apache server.
    But the problem is I cannot build a NSURL with a string like this :
    "http://toto.fr:80/script.php?v=value@/^777"

    How can I do to encode value@/^777 by %xx as urlEncode done !

    Eric

    Thanks You very much.
  • On Saturday, December 15, 2001, at 08:42  PM, Circlaeys Eric wrote:
    > Just a question for you,
    > I am looking for using NSURL to send a request to an apache server.
    > But the problem is I cannot build a NSURL with a string like this :
    > "http://toto.fr:80/script.php?v=value@/^777"
    >
    > How can I do to encode value@/^777 by %xx as urlEncode done !

    I didn't find anything in Foundation for this. It would be a useful
    addition.
    This should work:

    @implementation NSString (URLEncoding)

    static NSArray *urlCodes;

    - (NSArray *)urlCodes
    {
      // See: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
      if (!urlCodes)
      {
      NSMutableArray *c = [NSMutableArray array];
      urlCodes = c;
      [c addObject:[NSArray arrayWithObjects:@"%", @"%25", nil]];
      [c addObject:[NSArray arrayWithObjects:@" ", @"%20", nil]];
      [c addObject:[NSArray arrayWithObjects:@"/", @"%22", nil]];
      [c addObject:[NSArray arrayWithObjects:@" ", @"%20", nil]];
      [c addObject:[NSArray arrayWithObjects:@">", @"%3E", nil]];
      [c addObject:[NSArray arrayWithObjects:@"}", @"%7D", nil]];
      [c addObject:[NSArray arrayWithObjects:@"\\", @"%5C", nil]];
      [c addObject:[NSArray arrayWithObjects:@"~", @"%7E", nil]];
      [c addObject:[NSArray arrayWithObjects:@"]", @"%5D", nil]];
      [c addObject:[NSArray arrayWithObjects:@"[", @"%5B", nil]];
      [c addObject:[NSArray arrayWithObjects:@";", @"%3B", nil]];
      [c addObject:[NSArray arrayWithObjects:@"?", @"%3F", nil]];
      [c addObject:[NSArray arrayWithObjects:@"@", @"%40", nil]];
      [c addObject:[NSArray arrayWithObjects:@"&", @"%26", nil]];
      [c addObject:[NSArray arrayWithObjects:@"<", @"%3C", nil]];
      [c addObject:[NSArray arrayWithObjects:@"#", @"%23", nil]];
      [c addObject:[NSArray arrayWithObjects:@"{", @"%7B", nil]];
      [c addObject:[NSArray arrayWithObjects:@"}", @"%7D", nil]];
      [c addObject:[NSArray arrayWithObjects:@"|", @"%7C", nil]];
      [c addObject:[NSArray arrayWithObjects:@"^", @"%5E", nil]];
      [c addObject:[NSArray arrayWithObjects:@"`", @"%60", nil]];
      [c addObject:[NSArray arrayWithObjects:@"/", @"%2F", nil]];
      [c addObject:[NSArray arrayWithObjects:@":", @"%3A", nil]];
      [c addObject:[NSArray arrayWithObjects:@"=", @"%3D", nil]];
      [c addObject:[NSArray arrayWithObjects:@"$", @"%24", nil]];
      }
      return urlCodes;
    }

    - (NSString *)urlEncoded
    {
      NSString *s = self;
      NSArray *codes = [self urlCodes];
      NSEnumerator *enm = [codes objectEnumerator];
      NSArray *code;
      while (code = [enm nextObject])
      {
        NSString *s1 = [code objectAtIndex:0];
        NSString *s2 = [code objectAtIndex:1];
        s = [s stringByReplacingSubstring:s1 withString:s2];
      }
      return s;
    }

    - (NSString *)urlDecoded
    {
      NSString *s = self;
      NSArray *codes = [self urlCodes];
      NSEnumerator *enm = [codes objectEnumerator];
      NSArray *code;
      while (code = [enm nextObject])
      {
        NSString *s1 = [code objectAtIndex:1];
        NSString *s2 = [code objectAtIndex:0];
        s = [s stringByReplacingSubstring:s1 withString:s2];
      }
      return s;
    }

    - (NSString *)stringByReplacingSubstring:(NSString *)substring
      withString:(NSString *)replaceString
    {
      NSMutableString *string = [NSMutableString stringWithString:self];
      NSRange range;
      if ( !replaceString || !substring ) { return self; }
      range = [string rangeOfString:substring];
      while ( range.length )
      {
        [string replaceCharactersInRange:range withString:replaceString];
        //range.location = range.location + range.length;
        range.location = range.location + 1;
        range.length = [string length] - range.location;
        range = [string rangeOfString:substring options:0 range:range];
      }
      return string;
    }

    @end
  • Hey Steve,

    Sorry about this, but... :-)

    On Saturday, December 15, 2001, at 11:34 PM, Steve Dekorte wrote:
    > I didn't find anything in Foundation for this. It would be a useful
    > addition.
    > This should work:
    >
    > [c addObject:[NSArray arrayWithObjects:@"/", @"%22", nil]];

    %22 is double-quote ("). A slash (/) is %2F.

    This approach (replacing each character individually with the escape
    code) is slow and prone to individual errors like this one. Plus you are
    missing all the unprintable characters below %20 and DEL at %7F.

    There's an implementation in OmniFoundation (NSString-OFExtensions) that
    you are welcome to use, that uses a table of safe characters and encodes
    the unsafe characters in hex. That way it only goes over each character
    in the string once, and handles all of ASCII.

    --Greg
  • On Sunday, December 16, 2001, at 01:31  AM, Greg Titus wrote:
    > Hey Steve,
    >
    > Sorry about this, but... :-)
    >
    > On Saturday, December 15, 2001, at 11:34 PM, Steve Dekorte wrote:
    >> I didn't find anything in Foundation for this. It would be a useful
    >> addition.
    >> This should work:
    >>
    >> [c addObject:[NSArray arrayWithObjects:@"/", @"%22", nil]];
    >
    > %22 is double-quote ("). A slash (/) is %2F.

    Thanks.

    Steve
  • On Saturday, December 15, 2001, at 11:42 PM, Circlaeys Eric wrote:

    > Just a question for you,
    > I am looking for using NSURL to send a request to an apache server.
    > But the problem is I cannot build a NSURL with a string like this :
    > "http://toto.fr:80/script.php?v=value@/^777"
    >
    > How can I do to encode value@/^777 by %xx as urlEncode done !
    >
    >
    > Eric
    >
    > Thanks You very much.
    >

    There's a function in CoreFoundation that will do exactly what you
    need:  CFURLCreateStringByAddingPercentEscapes

    You could use it like so:

    NSString* myURL=@"http://toto.fr:80/script.php?v=value@/^777";

    NSString* escapedURL=[(NSString*)
    CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)myURL, NULL,
    NULL, kCFStringEncodingUTF8) autorelease];

    Hope this helps.

    -Peter
  • Thanks all

    > On Saturday, December 15, 2001, at 11:42 PM, Circlaeys Eric wrote:
    >
    >> Just a question for you,
    >> I am looking for using NSURL to send a request to an apache server.
    >> But the problem is I cannot build a NSURL with a string like this :
    >> "http://toto.fr:80/script.php?v=value@/^777"
    >>
    >> How can I do to encode value@/^777 by %xx as urlEncode done !
    >>
    >>
    >> Eric
    >>
    >> Thanks You very much.
    >>
    >
    > There's a function in CoreFoundation that will do exactly what you
    > need:  CFURLCreateStringByAddingPercentEscapes
    >
    > You could use it like so:
    >
    > NSString* myURL=@"http://toto.fr:80/script.php?v=value@/^777";
    >
    > NSString* escapedURL=[(NSString*)
    > CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)myURL, NULL,
    > NULL, kCFStringEncodingUTF8) autorelease];
    >
    > Hope this helps.
    >
    > -Peter
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
previous month december 2001 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today
MindNode
MindNode offered a free license !