IOS 网络请求中设置cookie
IOS网络请求中设置cookie
1.ASIHTTPRequest
ASIHTTPRequest是一款极其强劲的HTTP访问开源项目。让简单的API完成复杂的功能,如:异步请求,队列请求,GZIP压缩,缓存,断点续传,进度跟踪,上传文件,HTTP认证。
cookie的支持
如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。你可以用[ASIHTTPRequestsetSessionCookies:nil];清空所有Cookies。当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
-(NSMutableArray*)retrunCookies{ NSDictionary*properties=[[[NSMutableDictionaryalloc]init]autorelease]; [propertiessetValue:[LoginViewControllergetLanguageType:loginInfo.lang]forKey:NSHTTPCookieValue]; [propertiessetValue:@"BENGGURU.GAIA.CULTURE_CODE"forKey:NSHTTPCookieName]; [propertiessetValue:@""forKey:NSHTTPCookieDomain]; [propertiessetValue:[NSDatedateWithTimeIntervalSinceNow:60*60]forKey:NSHTTPCookieExpires]; [propertiessetValue:@""forKey:NSHTTPCookiePath]; NSHTTPCookie*cookie=[[[NSHTTPCookiealloc]initWithProperties:properties]autorelease]; return[NSMutableArrayarrayWithObject:cookie]; }
[requestsetRequestCookies:[selfretrunCookies]];//发送cookies,根据用户的选择,返回相应语言。
2. NSMutableURLRequest(可以用于webview)
NSDictionary*properties=[[[NSMutableDictionaryalloc]init]autorelease]; [propertiessetValue:userIdforKey:NSHTTPCookieValue]; [propertiessetValue:@"BENQGURU.GAIA.USERID"forKey:NSHTTPCookieName]; [propertiessetValue:@""forKey:NSHTTPCookieDomain]; [propertiessetValue:[NSDatedateWithTimeIntervalSinceNow:60*60]forKey:NSHTTPCookieExpires]; [propertiessetValue:@"/"forKey:NSHTTPCookiePath]; NSHTTPCookie*cookie=[[[NSHTTPCookiealloc]initWithProperties:properties]autorelease]; NSDictionary*properties1=[[[NSMutableDictionaryalloc]init]autorelease]; [properties1setValue:[LoginViewControllergetLanguageType:loginInfo.lang]forKey:NSHTTPCookieValue]; [properties1setValue:@"BENGGURU.GAIA.CULTURE_CODE"forKey:NSHTTPCookieName]; [properties1setValue:@""forKey:NSHTTPCookieDomain]; [properties1setValue:[NSDatedateWithTimeIntervalSinceNow:60*60]forKey:NSHTTPCookieExpires]; [properties1setValue:@"/"forKey:NSHTTPCookiePath]; NSHTTPCookie*cookie1=[[[NSHTTPCookiealloc]initWithProperties:properties1]autorelease]; NSArray*cookies=[NSArrayarrayWithObjects:cookie,cookie1,nil]; NSDictionary*headers=[NSHTTPCookierequestHeaderFieldsWithCookies:cookies]; NSMutableURLRequest*request=[NSMutableURLRequestrequestWithURL:[NSURLURLWithString:[objectvalueForKey:@"url"]]]; [requestsetValue:[headersobjectForKey:@"Cookie"]forHTTPHeaderField:@"Cookie"]; [webViewloadRequest:request];
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!