views:

120

answers:

1

I got objc_exception_throw when trying to set cookies for a ASIHTTPRequest request. I tried both of these but it didn't work out.

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];

NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];

[properties setValue:@".google.com" forKey:@"Domain"];
[properties setValue:@"/" forKey:@"path"];
[properties setValue:@"1600000000" forKey:@"expires"];


NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];

or replacing initiating code for cookie with this one

NSHTTPCookie *cookie = [[NSHTTPCookie alloc] init];

When I commented out the following line, everything worked fine.

[request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];

Can you guys tell me what the problem here!

+1  A: 

Don't know if this is your problem or not, but you should be using the defined NSHTTPCookie property keys like NSHTTPCookieDomain instead of literal strings like @"Domain".

You might be able to get more information about what's going wrong by catching the exception and logging it. For example:

@try {
    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];}
@catch (NSException *exception) {
    NSLog(@"Caught %@: %@", [exception name], [exception reason]);
}
@finally {
}
David Gelhar
David, thank for your advice. It's definitely look nicer using the defined property keys. However I couldn't catch the exception using your code. Did I miss something?
TuanCM
I'm wondering if my Macbook having problem or just my code. I tried to add just this line into a working code.NSHTTPCookie *cookie = [[NSHTTPCookie alloc] init];My app crashed with EXC_BAD_ACCESS error. This really confused me. Can anyone tell me why?
TuanCM