Wednesday, May 1, 2013

NSMutableURLRequest setAllHTTPHeaderFields example ios


setAllHTTPHeaderFields:

Replaces the receiver's header fields with the passed values.
- (void)setAllHTTPHeaderFields:(NSDictionary *)headerFields
Parameters of [NSMutableURLRequest setAllHTTPHeaderFields]
headerFields
A dictionary with the new header fields. HTTP header fields must be string values; therefore, each object and key in the headerFields dictionary must be a subclass of NSString. If either the key or value for a key-value pair is not a subclass of NSString, the key-value pair is skipped.
Example of [NSMutableURLRequest setAllHTTPHeaderFields]
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"domain.com", NSHTTPCookieDomain,
                            @"\\", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];
Example of [NSMutableURLRequest setAllHTTPHeaderFields]
- (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;
}