0

I am trying to have vertical pipes in the URL

Input String: http://testURL.com/Control?command=dispatch|HOME|ABC:User Name

-(NSString *)getURLEncodedString:(NSString *)stringvalue{

NSMutableString *output = [NSMutableString string];
const unsigned char *source = (const unsigned char *)[stringvalue UTF8String];
int sourceLen = strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
    const unsigned char thisChar = source[i];
    if (thisChar == ':' || thisChar == '/' || thisChar == '?' || thisChar == '=' || thisChar == '|' || thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
               (thisChar >= 'a' && thisChar <= 'z') ||
               (thisChar >= 'A' && thisChar <= 'Z') ||
               (thisChar >= '0' && thisChar <= '9')) {
        [output appendFormat:@"%c", thisChar];
    } else {
        [output appendFormat:@"%%%02X", thisChar];
    }
}
return output;
}

Output String after calling above method: http://testURL.com/Control?command=dispatch|HOME|ABC:User%20Name

Now, if I pass the above encode string to [[NSURL URLWithString:encodedString];

I am getting Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xae9d760 {NSUnderlyingError=0xaec8ed0 "bad URL", NSLocalizedDescription=bad URL}

Any input on this guys? I want the URL to look like the encodedString.

Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mobilewits
  • 1,743
  • 4
  • 21
  • 34
  • 1
    What are you going to do with it? '|' isn't a valid character in a URL, which is why it's being rejected as a bad URL. – gaige Jun 06 '13 at 14:15
  • possible duplicate of [vertical pipe symbol not being detected in NSURL](http://stackoverflow.com/questions/6167331/vertical-pipe-symbol-not-being-detected-in-nsurl) – Mobilewits Jun 06 '13 at 19:18

1 Answers1

2

I really cannot see a reason to encode/escape your string manually... Any way, this will work just fine:

NSString *urlString = @"http://testURL.com/Control?command=dispatch|HOME|ABC:User Name";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Which outputs:

http://testURL.com/Control?command=dispatch%7CHOME%7CABC:User%20Name

It seems that NSURL doesn't like vertical bars after all, which you didn't encode in your method and thus getting a bad URL code.

Alladinian
  • 34,483
  • 6
  • 89
  • 91