Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cookies from NSHTTPURLResponse

I've an extremely weird problem, I'm requesting a URL and I want to get the cookies from it, I've used this way to get the cookies:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSDictionary *fields = [HTTPResponse allHeaderFields];
    NSString *cookie = [fields valueForKey:"Set-Cookie"];
}

BUT the cookies is not complete, there is some field is missing, I've checked it on PostMan, all the cookies is there.

I've used also this method when initiating the NSURLRequest.

[request setHTTPShouldHandleCookies:YES];

Where is the problem?

NOTE: This problem is on iOS, I've an android version and it's working fine and all the cookies is there.

like image 473
Scar Avatar asked Sep 28 '13 11:09

Scar


4 Answers

Try this code:

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}
like image 139
biloshkurskyi.ss Avatar answered Nov 10 '22 08:11

biloshkurskyi.ss


@biloshkurskyi.ss answer is spot on.

I spent half a day trying to find out why some of my cookies were not appearing in my response.allHeaderFields on iOS but it was there on Android (using the same service).

The reason is because some cookies are extracted in advance and stored in the shared cookie store. They will not appear in allHeaderFields.

Here's the swift 3 version of the answer if anyone needs it:

let request = URLRequest(url: myWebServiceUrl)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
    (data, response, error) in
    if let error = error {
        print("ERROR: \(error)")
    } else {
        print("RESPONSE: \(response)")
        if let data = data, let dataString = String(data: data, encoding: .utf8) {
            print("DATA: " + dataString)
        }
        for cookie in HTTPCookieStorage.shared.cookies! {
            print("EXTRACTED COOKIE: \(cookie)") //find your cookie here instead of httpUrlResponse.allHeaderFields
        }
    }
})
task.resume()
like image 22
Travis M. Avatar answered Nov 10 '22 08:11

Travis M.


Did you tried following code sample, it should work:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSArray *cookies =[[NSArray alloc]init]; 
    cookies = [NSHTTPCookie 
        cookiesWithResponseHeaderFields:[response allHeaderFields] 
        forURL:[NSURL URLWithString:@""]]; // send to URL, return NSArray
}
like image 15
Ashwani Avatar answered Nov 10 '22 07:11

Ashwani


for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}

Second code is :

NSHTTPURLResponse *HTTPResponsesss = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponsesss allHeaderFields];
NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is your cookie

NSLog(@"%@", cookie);

Both codes work fine.

like image 2
Softlabsindia Avatar answered Nov 10 '22 07:11

Softlabsindia