0

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

lucgian841
  • 1,830
  • 5
  • 34
  • 57
  • i think this question is already been answered. You can use this link http://stackoverflow.com/questions/3423545/objective-c-iphone-percent-encode-a-string – Meenakshi Mar 25 '15 at 10:52

3 Answers3

7

I found a way to solve this issue, below is my code, maybe it should be useful for someone:

- (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");
    }

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"];

    NSString *encodedPassword = [password stringByAddingPercentEncodingWithAllowedCharacters:set];

    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", username, encodedPassword, 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;
}

In this way I search in the password string the special characters and if it found it change this character with the percent encoding. In that way I can log in with plain password and with password with special characters. I hope it will be useful for someone.

lucgian841
  • 1,830
  • 5
  • 34
  • 57
2

Just encode every parmeter with the - stringByAddingPercentEscapesUsingEncoding:.

NSString *encodeUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodePassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodeInstallationId = [installationId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&installationId=%@", encodeUsername, encodePassword, ins encodeInstallationId allationId];

And use this encoded paramater in your request.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
-1

Url encode your string and POST to web service. Please use below method for url encoding.

+(NSString *) urlencode: (NSString *) str {
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                               NULL,
                                                                               (CFStringRef)str,
                                                                               NULL,
                                                                               (CFStringRef)@" ",
                                                                               kCFStringEncodingUTF8 );

return [encodedString autorelease];

}

prabakaran iOS
  • 681
  • 7
  • 19