IOS 网络请求中设置cookie
2019/7/9 22:54:53
本文主要是介绍IOS 网络请求中设置cookie,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
IOS 网络请求中设置cookie
1. ASIHTTPRequest
ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目。让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度跟踪,上传文件,HTTP 认证。
cookie的支持
如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,并供下次使用。你可以用 [ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有 Cookies。当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
-(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]; }
[request setRequestCookies:[self retrunCookies]]; //发送cookies,根据用户的选择,返回相应语言。
2. NSMutableURLRequest(可以用于webview)
NSDictionary *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];
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇关于IOS 网络请求中设置cookie的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码