Create a cookie for NSURLRequest?

后端 未结 5 1648
佛祖请我去吃肉
佛祖请我去吃肉 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: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];
    

提交回复
热议问题