My app calls a web service to login. Usually the service works without any problems, but when I've a password with a special characters (example: !*'\"();:@&=+$,/?%#[]%), the web server doesn't allow me to login because on server side I receive the password without special characters for example:
password insert on my app: test+test
password receive to web server: testtest
As you see the request delete the special characters actually I'm sending the login credential by using the following code:
- (id)sendRequestToURL:(NSString *)url withMethod:(NSString *)method withUsername:(NSString*)username withPassword:(NSString*)password andInstallationId:(NSString*)installationId {
NSURL *finalURL = [[NSURL alloc]init];
if ([method isEqualToString:@"POST"]) {
finalURL = [NSURL URLWithString:url];
} else {
NSLog(@"Metodo no previsto");
}
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", username, password, installationId];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)postData.length];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:finalURL];
[request setHTTPMethod:method];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
[connection start];
}
return connection;
}
How I can fix this issue? I looked at the NSString class reference, I saw there's the method - stringByAddingPercentEncodingWithAllowedCharacters:
to don't delete the special characters, does anyone know how to use this method? Can you show me a code snippet?
Thank you