码迷,mamicode.com
首页 > 其他好文 > 详细

AFN传入cookie

时间:2016-05-20 22:26:20      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

//发送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

标签:

原文地址:http://www.cnblogs.com/arenouba/p/5513543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!