首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSMutableURLRequest请求

使用NSMutableURLRequest请求
EN

Stack Overflow用户
提问于 2012-07-07 11:20:23
回答 3查看 4.1K关注 0票数 2

我试着做一个登录页,让用户通过网站的认证。我试过这样的方法:

代码语言:javascript
复制
    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.'

有人能告诉我我哪里做错了吗?

谢谢你的帮助

EN

回答 3

Stack Overflow用户

发布于 2012-07-07 11:44:25

为什么不使用like

代码语言:javascript
复制
[request setValue:usernameField.text forHTTPHeaderField:@"username"];
[request setValue:passwordField.text forHTTPHeaderField:@"passwd"];

希望这对你有帮助。

票数 4
EN

Stack Overflow用户

发布于 2012-07-07 11:25:43

NSMutableURLRequest没有username属性(因此出现错误)。您需要执行如下操作(尽管这取决于您的服务器所期望的格式):

代码语言:javascript
复制
[request setHTTPMethod:@"POST"];
NSString* str = [NSString stringWithFormat:@"username=%@&passwd=%@", usernameField.text, passwordField.text];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2012-07-07 11:25:28

提示在您的错误字符串中:

此类与密钥用户名的键值编码不兼容。

我很确定你不能用你正在尝试的方式做基本的身份验证。快速搜索发现了this,我确实记得在尝试将其与每个请求一起传递时必须执行base64 (另一种方法是实现连接委托方法,并正确处理身份验证请求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11371985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档