IOS 网络请求中设置cookie
2015-08-18 16:45
573 查看
1. ASIHTTPRequest
ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目。让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度跟踪,上传文件,HTTP 认证。cookie的支持
如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,并供下次使用。你可以用 [ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有 Cookies。当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
[plain] view plaincopy
-(NSMutableArray*)retrunCookies{
NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
[properties setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
[properties setValue:@"" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
return [NSMutableArray arrayWithObject:cookie];
[plain] view plaincopy
}
[plain] view plaincopy
[request setRequestCookies:[self retrunCookies]]; //发送cookies,根据用户的选择,返回相应语言。
2. NSMutableURLRequest(可以用于webview)
[plain] view plaincopyNSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
[properties setValue:userId forKey:NSHTTPCookieValue];
[properties setValue:@"BENQGURU.GAIA.USERID" forKey:NSHTTPCookieName];
[properties setValue:@"" forKey:NSHTTPCookieDomain];
[properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties setValue:@"/" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
NSDictionary *properties1 = [[[NSMutableDictionary alloc] init] autorelease];
[properties1 setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
[properties1 setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
[properties1 setValue:@"" forKey:NSHTTPCookieDomain];
[properties1 setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
[properties1 setValue:@"/" forKey:NSHTTPCookiePath];
NSHTTPCookie *cookie1 = [[[NSHTTPCookie alloc] initWithProperties:properties1] autorelease];
NSArray *cookies=[NSArray arrayWithObjects:cookie,cookie1,nil];
NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[object valueForKey:@"url"]]];
[request setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
[webView loadRequest:request];
相关文章推荐
- NSHTTPCookie类详解
- C#网络编程
- Flex HTTPService
- Flex HTTPService
- PHP的一个简单封装的HTTP类
- TCP粘包的拆包处理
- [iOS]导入ASIHttpRequest遇到的问题
- 图解http 学习
- iOS开发系列--网络开发
- 使用HttpSessionListener接口监听Session的创建和失效
- TCP/IP协议学习(二) LWIP用户自定义配置文件解析
- iOS经典网络开源库(整理)
- http://zhongfox.github.io/blog/server/2013/05/12/nginx-var/
- 网络工程师联盟官方QQ群号:219372057
- 常用的网络营销方法有哪些
- TCP控制字段标志:URG、ACK、PSH、RST、SYN、FIN
- UNIX网络编程5 一些相似函数的比较和使用
- (三)自定义不断转圈的圆形进度条的实现(主要在网络访问时候,提示用户在获取数据)
- Java Android HTTP实现总结
- 基于java的https双向认证,android上亦可用