I am trying to send information to a login web service but certain characters are giving me trouble.
For example, if the password is something like h+4hfP
, the web service rejects it because the plus sign ('+') is not properly encoded to %2B
.
The web service uses UTF-8 encoding so I have been building an NSData object with this NSString method, - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
choosing NSUTF8StringEncoding
as the encoding.
This doesn't seem to be enough though.
The problem could also be with how I'm building an NSMutableURLRequest
:
NSData *postData = [@"h+4hfP" dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:contentLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
How can I ensure the encoding is done properly?