AFNetworking发送post请求时怎么设置cookie

 我来答
一骑当后
推荐于2016-03-15 · 知道合伙人数码行家
一骑当后
知道合伙人数码行家
采纳数:40298 获赞数:306438
网络、设备维护、电路、弱电检测。

向TA提问 私信TA
展开全部
//发送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];

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式