Create a cookie for NSURLRequest?

后端 未结 5 1641
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 22:05

I\'m trying to send an authentication string via cookie in a NSMutableURLRequest. I\'m trying to create the NSHTTPCookie through

 +(id)cookieWithProperties:(         


        
相关标签:
5条回答
  • 2020-12-22 22:37

    I noticed on, on my 2.2.1 iphone, that the cookie didn't get created if NSHTTPCookiePath is not specified, even though it is shown as "optional" in the docs:

    So, I do:

    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];
    
    0 讨论(0)
  • 2020-12-22 22:42

    I've found one mistake in jm's example: NSHTTPCookiePath should be @"/", but not @"\\\\".

    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];
    
    0 讨论(0)
  • 2020-12-22 22:45

    This is how you set properties in a cookie:

     NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                  url, NSHTTPCookieOriginURL,
                                  @"testCookies", NSHTTPCookieName,
                                  @"1", NSHTTPCookieValue,
                                  nil];
      NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
    

    In the example above: url, testCookies, and 1 are the values. Likewise, NSHTTPCookieOriginURL, NSHTTPCookieName, NSHTTPCookieValue are the keys for the NSDictionary object, as in key-value pairs.

    You set/get properties using NSDictionary and add to NSHTTPCookie.

    0 讨论(0)
  • 2020-12-22 22:51

    I could not get that to work.

    I got this to work however:

    NSMutableURLRequest* ret = [NSMutableURLRequest requestWithURL:myURL];
    [ret setValue:@"myCookie=foobar" forHTTPHeaderField:@"Cookie"];
    0 讨论(0)
  • 2020-12-22 22:56

    key NSHTTPCookiePath should exist in dictionary when using

    [NSHTTPCookie cookieWithProperties:dictionary]
    

    method whether using NSHTTPCookieDomain or NSHTTPCookieOriginURL. And value for NSHTTPCookiePath should be @"/" not @"\\".

    0 讨论(0)
提交回复
热议问题