IOS 网络请求中设置cookie
程序员文章站
2023-12-22 08:44:58
ios 网络请求中设置cookie
1. asihttprequest
asihttprequest 是一款极其强劲的 http 访问开源项目。让简单的 api 完...
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];
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!