lucgian841
lucgian841

Reputation: 2002

Send POST request with special characters iOS

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

Upvotes: 0

Views: 4978

Answers (3)

lucgian841
lucgian841

Reputation: 2002

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.

Upvotes: 7

rckoenes
rckoenes

Reputation: 69479

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.

Upvotes: 3

prabakaran iOS
prabakaran iOS

Reputation: 681

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];

}

Upvotes: -1

Related Questions