首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSHTTPCookie的问题

NSHTTPCookie的问题
EN

Stack Overflow用户
提问于 2011-05-11 14:37:54
回答 1查看 3.5K关注 0票数 2

我想做一个登录申请。我的第一个屏幕登录表单有两个文本字段和一个按钮。当我按下这个按钮时,我会调用一个调用此方法的方法:

代码语言:javascript
复制
- (int)login {
// Add data to post request

NSHTTPURLResponse * response;
NSString *myRequestString = [[NSString alloc] initWithFormat:@"userdata='%@'&passdata='%@'",
                             username.text, password.text];
NSError * error;
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request;
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                    timeoutInterval:60] autorelease];

[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  

NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://server.com/login.php"]];

//int cid;

for (NSHTTPCookie *cookie in all) {
    NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value); 
//    cid = (int)cookie.value;
}

//  NSLog(@"id: %d",cid);

[myRequestString release];
[request release];

return 1;
}

当我按下这个按钮时,我的程序崩溃了,并且在这一行旁边:

代码语言:javascript
复制
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://sms.britecs.com/login.php"]];

我有Thread1: Program received signal: "EXC_BAD_ACCESS",但不知道如何修复它。

我还有一个问题。如何在下一个屏幕中使用此cookies?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-11 14:56:25

使用[request release];,您将释放一个自动释放的对象。不要这样做,它将由自动释放池在下一个运行循环周期中释放。在初始化结束时删除自动释放,这样就可以了,否则删除release语句。

您可以在这里创建它:

代码语言:javascript
复制
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://server.com/login.php"]
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                    timeoutInterval:60] autorelease]; // <---
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5960211

复制
相关文章

相似问题

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