I have to login with my to a web-server.All works fine, but not generated passwords like |%<">{}¥^~ . How I have to encode passwords like this?
I create a User with password=|%<">{}¥^~ doset work ( for example a password like "user1234" works fine)
NSString *userName = self.usernameOutlet.text;
NSString *userPassword = self.passwordOutlet.text;
NSString *escapedString = [self.passwordOutlet.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
userPassword = escapedString;
NSString *post = [NSString stringWithFormat:@"login=%@&password=%@",userName,userPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://XXXXXXXX/login"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
with passwords like "user1234" I get a cookie
with passwords like "|%<">{}¥^~" I get no cookie
what am I doing wrong?
NSURLQueryAllowedCharacterSet
won’t quite work either. It will let&
and+
pass unescaped (because those are technically allowed in the “query” portion of a URL, but not in a value (or key) of ax-www-form-urlencoded
payload). – Catling"foo+bar&baz"
. It won’t escape the+
or the&
. – CatlingNSURLConnection
is now deprecated. We’d generally useNSURLSession
nowadays... – Catling