简体   繁体   English

如何在AFNetworking 2.0+中使用AFHTTPRequestOperationManager使用cookie?

[英]How can I use cookies using AFHTTPRequestOperationManager in AFNetworking 2.0+?

As it is known, AFHTTPSessionManager in AFNetworking 2.0+ supports cookies. 正如众所周知,AFHTTPSessionManager在AFNetworking 2.0+支持Cookie。

But is it possible for AFHTTPRequestOperationManager in AFNetworking 2.0+ to support cookies? 但是,它可能AFHTTPRequestOperationManager在AFNetworking 2.0+支持cookies吗?

Yes. 是。 AFNetworking uses the foundation URL Loading system, which handles cookies out of the box. AFNetworking使用基础URL加载系统,它可以开箱即用地处理cookie。

You can configure NSMutableURLRequest's setHTTPShouldHandleCookies and use NSHTTPCookieStorage to store them. 您可以配置NSMutableURLRequest的setHTTPShouldHandleCookies并使用NSHTTPCookieStorage来存储它们。

In Objective-C: 在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];
}

In Swift: 在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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将AFNetworking 2.0+与旧的AFNetworking一起使用? - How to use AFNetworking 2.0+ with old AFNetworking? AFNetworking ASPXAUTH Cookies AFHTTPRequestOperationManager - AFNetworking ASPXAUTH Cookies AFHTTPRequestOperationManager AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy无法正常工作 - AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy not working 如何使用参数上传图像时如何使用AFNetworking 2.0设置cookie? - How can I set cookies with AFNetworking 2.0 while uploading image with parameteres? AFNetworking 2.0:AFHTTPRequestOperationManager异步方法调用 - AFNetworking 2.0 : AFHTTPRequestOperationManager asynchronous methode invoke AFNetworking 2.0将一批AFHTTPRequestOperation对象添加到AFHTTPRequestOperationManager - AFNetworking 2.0 Add batch of AFHTTPRequestOperation objects to AFHTTPRequestOperationManager 如何使用AFNetworking AFHTTPRequestOperationManager显示ProgressBar - How to show ProgressBar With AFNetworking AFHTTPRequestOperationManager 无法使用AFHTTPRequestOperationManager - Can't use AFHTTPRequestOperationManager 如何记录AFHTTPRequestOperationManager请求? - How can I log AFHTTPRequestOperationManager request? 如何在afnetworking中获取AFHTTPRequestOperationManager请求中的返回 - how to get return in AFHTTPRequestOperationManager request in afnetworking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM