Problems instantiating an URL

  • Hello,

    I fail to see why instantiating the following URL fails. Here's the code:

            NSMutableString * googleAddress = [NSMutableString stringWithString:[self stringValue]];

            // Prepare the address in a format that Google understands...
            [googleAddress replaceOccurrencesOfString:@"\n" withString:@"+" options:nil range:NSMakeRange(0, [googleAddress length])];
            [googleAddress replaceOccurrencesOfString:@" " withString:@"+" options:nil range:NSMakeRange(0, [googleAddress length])];

            NSLog(@"%@", googleAddress);
            NSURL *url = [NSURL URLWithString:googleAddress];
            NSLog(@"%@", url);
            [[NSWorkspace sharedWorkspace]openURL:url];

    This is the output:

    2006-10-07 18:07:21.563 Test[14980:117] http://maps.google.com/maps?q=4720+Forbes+Avenue,Suite+420+Pittsburgh?+Penn
    sylvania?+15213+United+States

    2006-10-07 18:07:21.564 Test[14980:117] (null)

    If you paste the URL above in Google maps, it works fine. However, URLWithString: returns nil. Any idea why?

    Thanks in advance,

    -- Tito
  • > http://maps.google.com/maps?q=4720+Forbes+Avenue,Suite+420
    > +Pittsburgh?+Pennsylvania?+15213+United+States
    >
    > If you paste the URL above in Google maps, it works fine. However,
    > URLWithString: returns nil. Any idea why?

    I'm guessing here, but what happens if you remove the extra question
    marks?

    Stephen Deken
    <stephen.deken...>
  • What's up with the question marks after Pittsburgh and Pennsylvania?
    I'd be suspicious of that. A URL with multiple question marks doesn't
    seem legal to me. It's probably indicative of a problem somewhere, but
    even assuming you wanted them there, you'd need to percent-escape them.

    Tito Ciuro wrote:
    > Hello,
    >
    > I fail to see why instantiating the following URL fails. Here's the code:
    >
    > NSMutableString * googleAddress = [NSMutableString stringWithString:[self stringValue]];
    >
    > // Prepare the address in a format that Google understands...
    > [googleAddress replaceOccurrencesOfString:@"\n" withString:@"+" options:nil range:NSMakeRange(0, [googleAddress length])];
    > [googleAddress replaceOccurrencesOfString:@" " withString:@"+" options:nil range:NSMakeRange(0, [googleAddress length])];
    >
    > NSLog(@"%@", googleAddress);
    > NSURL *url = [NSURL URLWithString:googleAddress];
    > NSLog(@"%@", url);
    > [[NSWorkspace sharedWorkspace]openURL:url];
    >
    > This is the output:
    >
    > 2006-10-07 18:07:21.563 Test[14980:117] http://maps.google.com/maps?q=4720+Forbes+Avenue,Suite+420+Pittsburgh?+Penn
    sylvania?+15213+United+States

    > 2006-10-07 18:07:21.564 Test[14980:117] (null)
    >
    > If you paste the URL above in Google maps, it works fine. However, URLWithString: returns nil. Any idea why?
    >
    > Thanks in advance,
    >
    > -- Tito
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<jstiles...>
    >
    > This email sent to <jstiles...>
    >
  • Hello again,

    The extra question marks? My bad. Sorry about that. I have removed
    them and it still fails. The URL looks like this now:

    http://maps.google.com/maps?q=4720+Forbes+Avenue,Suite+420+Pittsburgh
    +Pennsylvania+15213+United+States

    Thanks Stephen and John for your quick reply.

    -- Tito

    On Oct 7, 2006, at 6:39 PM, John Stiles wrote:

    > What's up with the question marks after Pittsburgh and Pennsylvania?
    > I'd be suspicious of that. A URL with multiple question marks
    > doesn't seem legal to me. It's probably indicative of a problem
    > somewhere, but even assuming you wanted them there, you'd need to
    > percent-escape them.
    >
    >
    > Tito Ciuro wrote:
    >> Hello,
    >>
    >> I fail to see why instantiating the following URL fails. Here's
    >> the code:
    >>
    >> NSMutableString * googleAddress = [NSMutableString
    >> stringWithString:[self stringValue]];
    >> // Prepare the address in a format that Google
    >> understands...
    >> [googleAddress replaceOccurrencesOfString:@"\n"
    >> withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    >> length])];
    >> [googleAddress replaceOccurrencesOfString:@" "
    >> withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    >> length])];
    >> NSLog(@"%@", googleAddress);
    >> NSURL *url = [NSURL URLWithString:googleAddress];
    >> NSLog(@"%@", url);
    >> [[NSWorkspace sharedWorkspace]openURL:url];
    >>
    >> This is the output:
    >>
    >> 2006-10-07 18:07:21.563 Test[14980:117] http://maps.google.com/
    >> maps?q=4720+Forbes+Avenue,Suite+420+Pittsburgh?+Pennsylvania?+15213
    >> +United+States
    >> 2006-10-07 18:07:21.564 Test[14980:117] (null)
    >>
    >> If you paste the URL above in Google maps, it works fine. However,
    >> URLWithString: returns nil. Any idea why?
    >>
    >> Thanks in advance,
    >>
    >> -- Tito
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/jstiles%
    >> 40blizzard.com
    >>
    >> This email sent to <jstiles...>
    >>
  • Why not use stringByAddingPerfectEscapesUsingEncoding: rather than
    your own replacing code?

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
    Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/
    NSString/stringByAddingPercentEscapesUsingEncoding:

    That will ensure the string is valid for a URL.

    On Oct 7, 2006, at 6:11 PM, Tito Ciuro wrote:

    > Hello,
    >
    > I fail to see why instantiating the following URL fails. Here's the
    > code:
    >
    > NSMutableString * googleAddress = [NSMutableString
    > stringWithString:[self stringValue]];
    >
    > // Prepare the address in a format that Google understands...
    > [googleAddress replaceOccurrencesOfString:@"\n"
    > withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    > length])];
    > [googleAddress replaceOccurrencesOfString:@" "
    > withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    > length])];
    >
    > NSLog(@"%@", googleAddress);
    > NSURL *url = [NSURL URLWithString:googleAddress];
    > NSLog(@"%@", url);
    > [[NSWorkspace sharedWorkspace]openURL:url];
    >
    > This is the output:
    >
    > 2006-10-07 18:07:21.563 Test[14980:117] http://maps.google.com/maps?
    > q=4720+Forbes+Avenue,Suite+420+Pittsburgh?+Pennsylvania?+15213
    > +United+States
    > 2006-10-07 18:07:21.564 Test[14980:117] (null)
    >
    > If you paste the URL above in Google maps, it works fine. However,
    > URLWithString: returns nil. Any idea why?
    >
    > Thanks in advance,
    >
    > -- Tito
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<enigma0...>
    >
    > This email sent to <enigma0...>
  • On 07 Oct 06, at 18:39, John Stiles wrote:
    > What's up with the question marks after Pittsburgh and Pennsylvania?
    > I'd be suspicious of that. A URL with multiple question marks
    > doesn't seem legal to me. It's probably indicative of a problem
    > somewhere, but even assuming you wanted them there, you'd need to
    > percent-escape them.

    Unusual, but not illegal. URLs with multiple question marks are
    entirely possible; the interpretation depends entirely on the server.
  • Bingo! :-)

    Thanks Ryan! It works fine now.

    -- Tito

    On Oct 7, 2006, at 7:21 PM, Ryan Britton wrote:

    > Why not use stringByAddingPerfectEscapesUsingEncoding: rather than
    > your own replacing code?
    >
    > http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
    > Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/
    > instm/NSString/stringByAddingPercentEscapesUsingEncoding:
    >
    > That will ensure the string is valid for a URL.
    >
    > On Oct 7, 2006, at 6:11 PM, Tito Ciuro wrote:
    >
    >> Hello,
    >>
    >> I fail to see why instantiating the following URL fails. Here's
    >> the code:
    >>
    >> NSMutableString * googleAddress = [NSMutableString
    >> stringWithString:[self stringValue]];
    >>
    >> // Prepare the address in a format that Google understands...
    >> [googleAddress replaceOccurrencesOfString:@"\n"
    >> withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    >> length])];
    >> [googleAddress replaceOccurrencesOfString:@" "
    >> withString:@"+" options:nil range:NSMakeRange(0, [googleAddress
    >> length])];
    >>
    >> NSLog(@"%@", googleAddress);
    >> NSURL *url = [NSURL URLWithString:googleAddress];
    >> NSLog(@"%@", url);
    >> [[NSWorkspace sharedWorkspace]openURL:url];
    >>
    >> This is the output:
    >>
    >> 2006-10-07 18:07:21.563 Test[14980:117] http://maps.google.com/
    >> maps?q=4720+Forbes+Avenue,Suite+420+Pittsburgh?+Pennsylvania?+15213
    >> +United+States
    >> 2006-10-07 18:07:21.564 Test[14980:117] (null)
    >>
    >> If you paste the URL above in Google maps, it works fine. However,
    >> URLWithString: returns nil. Any idea why?
    >>
    >> Thanks in advance,
    >>
    >> -- Tito
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/<enigma0...>
    >>
    >> This email sent to <enigma0...>
    >
  • On 08/10/2006, at 12:21 PM, Andrew Farmer wrote:

    > On 07 Oct 06, at 18:39, John Stiles wrote:
    >> What's up with the question marks after Pittsburgh and Pennsylvania?
    >> I'd be suspicious of that. A URL with multiple question marks
    >> doesn't seem legal to me. It's probably indicative of a problem
    >> somewhere, but even assuming you wanted them there, you'd need to
    >> percent-escape them.
    >
    > Unusual, but not illegal. URLs with multiple question marks are
    > entirely possible; the interpretation depends entirely on the server.

    My understanding is that question marks within the query part of a URI
    are reserved and it depends upon the scheme as to whether they are
    legal. The http scheme reserves the use of "?" and ";" in the query/
    search part.

    - Chris
previous month october 2006 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