带有百分比符号的密码始终是错误的iPhone [英] password with percentage symbol always wrong iPhone
问题描述
我正在使用具有登录功能的Xcode开发iPhone应用程序,但是在使用以下代码时遇到了麻烦:
I'm developing an iPhone application in Xcode with a login function and I'm having trouble with the following code:
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",[NSString stringWithFormat:@"%@",username],password];
NSLog(post);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
[request setHTTPMethod:@"POST"];
[request addValue:[NSString stringWithFormat:@"%@",username] forHTTPHeaderField:@"username"];
[request addValue:password forHTTPHeaderField:@"password"];
[request setHTTPBody:postData];
action = @"token";
NSURLConnection *connection;
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
现在有一个成员,其密码为%32dzs3 *,并且该应用程序始终会给出密码不正确的错误.当我NSLog密码时,实际上不是我在文本字段中键入的密码.它看起来像这样:50883393zs3 *.我也尝试过这个:
Now there is a member with the password %32dzs3* and the app always gives an error that the password is incorrect. When I NSLog the password it's indeed not the password I typed in the textfield. it looked like this: 50883393zs3* . I also tried this:
for(int i = 0; i < [password length];i++){
if([password characterAtIndex:i] == '%'){
NSString *temporarypw = [password substringWithRange:NSMakeRange(0, i)];
password = [NSString stringWithFormat:@"%@%%%%%@",temporarypw,[password substringWithRange:NSMakeRange((i+1), (password.length - i -1))]];
break;
}
}
这将以%%%% 32dzs3 *的形式返回密码,并且在我NSLogLog时密码正确.当我运行请求时,响应仍然是错误的密码响应.我该如何解决?
this returns the password as %%%%32dzs3* and the password is right when I NSLog it. still the response is a wrong password response when I run the request. How can I fix this?
推荐答案
%
是HTTP URL中的特殊字符.您需要方法来做到这一点.
%
is a special character in HTTP URLs. You need to escape it before creating the URL, also :
, /
, #
, ;
, and @
. Fortunately NSString
has a method to do it.
这篇关于带有百分比符号的密码始终是错误的iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!