AFN传入cookie

Posted aRenOuBa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AFN传入cookie相关的知识,希望对你有一定的参考价值。

//发送post请求的同时传入cookie
+ (void)requestCookieWithPath:(NSString *)path
Params:(NSDictionary *)params
Method:(NSString *)method
Success:(HttpSuccessBlock)success{

//创建post请求

//创建AFHTTPClient对象
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kBaseUrl]];

NSMutableURLRequest *post = [client requestWithMethod:method path:path parameters:params];

NSData *cookiesData = [[NSUserDefaults standardUserDefaults]objectForKey:@"Set-Cookie"];
if ([cookiesData length]) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
NSHTTPCookie *cookie;
for (cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookie:cookie];
}
}

//创建AFJSONRequestOperation对象
NSOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:post success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

success(JSON);

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

NSLog(@"error = %@",error);
//请求超时提示
NSString *errorStr = [[NSString alloc]initWithFormat:@"%@",error];
NSString *theError = @"The request timed out.";
if ([errorStr rangeOfString:theError].length > 0) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请求超时" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}

}];
//开始请求
[operation start];

}

以上是关于AFN传入cookie的主要内容,如果未能解决你的问题,请参考以下文章

ios开发网络学习AFN三:AFN的序列化

iOS开发AFN使用二:AFN文件下载与文件上传

AFN小记

AFN框架内部结构

iOS开发之AFN的基本使用

AFN解析器里的坑