从response的HeaderField获得头文件,从头文件中通过NSHTTPCookie的cookiesWithResponseHeaderFields组成cookie的NSArray,将生成cookie的array,使用NSHttpCookie的reqeustHeaderFieldsWithCookies方法拼接成合法的http header field。最后set到request中即可。
[manager.requestSerializer setValue:[requestFields objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
使用以下代码进行请求,解析为cookie存入NSUserDefaults,全局单例类
[manager POST:requestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
/**
* 从response的HeaderField获得头文件,从头文件中通过NSHTTPCookie的cookiesWithResponseHeaderFields组成cookie的NSArray,将生成cookie的array,使用NSHttpCookie的reqeustHeaderFieldsWithCookies方法拼接成合法的http header field。最后set到request中即可。
[manager.requestSerializer setValue:[requestFields objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
*/
NSDictionary *fields= [operation.response allHeaderFields];
NSArray *cookies=[NSHTTPCookie cookiesWithResponseHeaderFields:fields forURL:[NSURL URLWithString:BaseURLString]];
NSDictionary* requestFields=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[[NSUserDefaults standardUserDefaults] setObject:[requestFields objectForKey:@"Cookie"] forKey:mUserDefaultsCookie];
// 使用jsonkit进行json解析
int result=[self jsonToUserDefault:operation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"Error: %@", error);
// 使用Bl的 loginfailed方法
[_delegate loginFail];
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue: [[NSUserDefaults standardUserDefaults] objectForKey:mUserDefaultsCookie]forHTTPHeaderField:@"Cookie"];
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];
NSString* suffix=[NSString stringWithFormat:@"?m=user&a=edit&nickname=%@&age=%@",nickName,age];
NSString* requestUrl =[BaseURLString stringByAppendingString:suffix];
NSString* utf8=[requestUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//将请求地址转换为utf8编码,使用默认unicode进行请求会报编码错误