正如众所周知,AFHTTPSessionManager在AFNetworking 2.0+支持Cookie.
但是,它可能AFHTTPRequestOperationManager在AFNetworking 2.0+支持cookies吗?
是.AFNetworking使用基础URL加载系统,它可以开箱即用地处理cookie.
您可以配置NSMutableURLRequest setHTTPShouldHandleCookies
并使用NSHTTPCookieStorage
它们来存储它们.
在Objective-C中:
NSArray *cookieStorage = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]; NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieStorage]; NSMutableURLRequest *request = [myRequestSerializer requestWith…]; for (NSString *key in cookieHeaders) { [request addValue:cookieHeaders[key] forHTTPHeaderField:key]; }
在Swift中:
var request = NSMutableURLRequest() // you can use an AFNetworking Request Serializer to create this if let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(url) { for (headerField, cookie) in NSHTTPCookie.requestHeaderFieldsWithCookies(cookieStorage) { request.addValue(cookie, forHTTPHeaderField: headerField) } }