|
From: | Richard Frith-Macdonald |
Subject: | Re: no subject |
Date: | Mon, 14 Apr 2008 12:10:41 +0100 |
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 usingNSURL. As the documentation says, NSURL needs an escaped string, so I usedstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding, so I wrotethe 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 = [NSStringstringWithFormat:@"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).
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; }
[Prev in Thread] | Current Thread | [Next in Thread] |