我试着做一个登录页,让用户通过网站的认证。我试过这样的方法:
NSURL *myURL = [NSURL URLWithString:@"https://www.freelancer.com/users/onlogin.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSLog(@"ispost");
[request setHTTPMethod:@"POST"];
[request setValue:usernameField.text forKey:@"username"];
[request setValue:passwordField.text forKey:@"passwd"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!theConnection) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Could not login!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}我得到一个错误,如下:
2012-07-07 10:09:37.354 Auth[6062:f803] ispost 2012-07-07 10:09:37.357 Auth[6062:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSMutableURLRequest 0x68d69d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key username.'
有人能告诉我我哪里做错了吗?
谢谢你的帮助
发布于 2012-07-07 11:44:25
为什么不使用like
[request setValue:usernameField.text forHTTPHeaderField:@"username"];
[request setValue:passwordField.text forHTTPHeaderField:@"passwd"];希望这对你有帮助。
发布于 2012-07-07 11:25:43
NSMutableURLRequest没有username属性(因此出现错误)。您需要执行如下操作(尽管这取决于您的服务器所期望的格式):
[request setHTTPMethod:@"POST"];
NSString* str = [NSString stringWithFormat:@"username=%@&passwd=%@", usernameField.text, passwordField.text];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];希望这能有所帮助。
发布于 2012-07-07 11:25:28
提示在您的错误字符串中:
此类与密钥用户名的键值编码不兼容。
我很确定你不能用你正在尝试的方式做基本的身份验证。快速搜索发现了this,我确实记得在尝试将其与每个请求一起传递时必须执行base64 (另一种方法是实现连接委托方法,并正确处理身份验证请求。
https://stackoverflow.com/questions/11371985
复制相似问题