【问题标题】:password with percentage symbol always wrong iPhone带有百分比符号的密码总是错误的 iPhone
【发布时间】:2012-01-12 13:34:53
【问题描述】:

我正在 Xcode 中开发一个带有登录功能的 iPhone 应用程序,但遇到以下代码问题:

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* 。我也试过这个:

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* 并且当我 NSLog 时密码是正确的。当我运行请求时,响应仍然是错误的密码响应。我该如何解决这个问题?

【问题讨论】:

  • 您的意思是在您的应用程序中,密码以明文形式发送而不是转义?哎哟。我不想成为你的用户之一。

标签: ios objective-c iphone login


【解决方案1】:

% 是 HTTP URL 中的特殊字符。您需要escape it before creating the URL:/#;@。幸运的是NSString 有一个method 可以做到这一点。

【讨论】:

    【解决方案2】:

    您可能需要正确转义密码。看到这个问题: NSString method to percent escape '&' for URL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多