[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: no subject
From: |
Sebastian Reitenbach |
Subject: |
Re: no subject |
Date: |
Mon, 14 Apr 2008 13:33:14 +0200 |
Richard Frith-Macdonald <richard@tiptree.demon.co.uk> wrote:
>
> On 14 Apr 2008, at 11:56, Richard Frith-Macdonald wrote:
>
> >
> > On 14 Apr 2008, at 10:41, Sebastian Reitenbach wrote:
> >
> >> Hi,
> >>
> >> I tried to download a csv file from a webserver.I found an example
> >> using
> >> NSURL.
> >> As the documentation says, NSURL needs an
> >> escaped string, so I used
> >> stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding, so
> >> I wrote
> >> the test below:
> >> =================Sample Program================================
> >> #include <Foundation/Foundation.h>
> >>
> >> int
> >> main(int argc, const char *argv[])
> >> {
> >> id pool = [[NSAutoreleasePool alloc] init];
> >>
> >> NSString *GKey =
> >>
@"ABQIAAAAvp3__HwvT3VkixIIbsW0axQuKI_6t1bH2P0vCI_Q8jfpn8qdNBQMnneljxh9czilkau_bYSCXteS_A
> >> ";
> >>
> >>
> >>
> >> NSString *Address = @"Potsdamer+Platz+Berlin+Germany";
> >> NSString *URLString = [NSString
> >> stringWithFormat:@"http://maps.google.com/maps/geo?q=
> >> %@&output=csv&key=%@",
> >> Address, GKey];
> >> URLString = [URLString
> >> stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
> >
> > That last line is wrong ... the method is for fixing strings which
> > are not legal as parts of a URL so that they can be used within a
> > URL ... if you apply the method to en entire URL, what you get is
> > obviously not a URL, but rather a string containing characters which
> > are legal to put inside a URL.
> > While the first sentence of the GNUstep documentation for the method
> > is perhaps unclear (I've tried to clarify it), the remainder of the
> > documentation goes on to explain exactly what the method does. It
> > also warns that, where you are filling in form fields (as in the
> > example above), you need to perform additional escaping for '+','='
> > and '&' because the implementation does not do that for you (to
> > maintain macos compatibility).
>
thanks for clarifying, then I obviously misunderstood it a bit.
>
> Something like the following function might be what you want for
> handling form data:
>
> NSString*
> URLFormField(NSString *value)
> {
> static NSCharacterSet *illegtal = nil;
> NSRange r;
>
> if (illegal == nil)
> {
> illegal = [[NSCharacterSet characterSetWithCharactersInString:
> @"+=&"] retain];
> }
> value = [value stringByAddingPercentEscapesUsingEncoding:
> NSUTF8StringEncoding];
> r = [value rangeOfCharacterFromSet: illegal];
> if (r.length > 0)
> {
> NSMutableString *m = [value mutableCopy];
>
> while (r.length > 0)
> {
> switch ([m characterAtIndex: r.location])
> {
> case '+': [m replaceCharactersInRange: r withString: @"%2B"];
> break;
> case '=': [m replaceCharactersInRange: r withString: @"%3D"];
> break;
> default: [m replaceCharactersInRange: r withString: @"%26"];
> break;
> }
> r = [m rangeOfCharacterFromSet: illegal];
> }
> value = [m autorelease];
> }
> return value;
> }
>
ah, thanks a lot for this example, seems very helpful.
kind regards
Sebastian
- no subject, Sebastian Reitenbach, 2008/04/14
- Re: no subject,
Sebastian Reitenbach <=