I'm doing a simple login and noticed that during the redirect I only have 2 of the 3 required cookies to get in correctly. I captured the other cookie and put them together but for some reason I can't modify the headers on the fly?
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
NSURL* redirected_url = [request URL];
NSString* querystr = [redirected_url absoluteString];
if (response != nil) {
NSArray* zzzz = [NSHTTPCookie
cookiesWithResponseHeaderFields:[response allHeaderFields]
forURL:[NSURL URLWithString:@""]];
if ([zzzz count] > 0) {
if ([querystr isEqualToString:@"https://www.localhost.com/specificurl.aspx"]) {
NSMutableArray* actualCookies = [[NSMutableArray alloc] init];
NSHTTPCookie* obj = [self.tmpCookies objectAtIndex:0];
开发者_开发技巧 [actualCookies addObject:obj];
[actualCookies addObject:zzzz];
NSArray* authToken = [[NSArray alloc] initWithArray:actualCookies];
//BLOWS UP HERE ?? NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:authToken];
//[request setAllHTTPHeaderFields:authToken];
[viewController setAuthCookieAfterValidLogin:zzzz];
}
}
}
return request;
}
The general idea is to set this header to have the value of my combined cookies
I found that although I couldn't modify the existing request, that didn't stop me from creating a new request and simply returning that one :)
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
NSURL* redirected_url = [request URL];
NSString* querystr = [redirected_url absoluteString];
if (response != nil) {
NSArray* zzzz = [NSHTTPCookie
cookiesWithResponseHeaderFields:[response allHeaderFields]
forURL:[NSURL URLWithString:@""]];
if ([zzzz count] > 0) {
if ([querystr isEqualToString:@"https://www.localhost.com/specificurl.aspx"]) {
NSMutableArray* actualCookies = [[NSMutableArray alloc] init];
NSUInteger i, count = [zzzz count];
for (i = 0; i < count; i++) {
NSHTTPCookie* xxx = [zzzz objectAtIndex:i];
[actualCookies addObject:xxx];
}
NSHTTPCookie* obj = [self.tmpCookies objectAtIndex:0];
[actualCookies addObject:obj];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:actualCookies];
NSURL *url = [NSURL URLWithString:@"https://www.localhost.com/specificurl.aspx"];
NSMutableURLRequest* xrequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[xrequest setHTTPMethod:@"GET"];
[xrequest setAllHTTPHeaderFields:headers];
[xrequest setValue:@"https://www.localhost.com/Default.aspx?Site_ID=500000" forHTTPHeaderField: @"Referer"];
[viewController setAuthCookieAfterValidLogin:zzzz];
return xrequest;
}
}
}
return request;
}
精彩评论