IOS 網(wǎng)絡(luò)請(qǐng)求中設(shè)置cookie
IOS 網(wǎng)絡(luò)請(qǐng)求中設(shè)置cookie
1. ASIHTTPRequest
ASIHTTPRequest 是一款極其強(qiáng)勁的 HTTP 訪問開源項(xiàng)目。讓簡(jiǎn)單的 API 完成復(fù)雜的功能,如:異步請(qǐng)求,隊(duì)列請(qǐng)求,GZIP 壓縮,緩存,斷點(diǎn)續(xù)傳,進(jìn)度跟蹤,上傳文件,HTTP 認(rèn)證。
cookie的支持
如果 Cookie 存在的話,會(huì)把這些信息放在 NSHTTPCookieStorage 容器共享,并供下次使用。你可以用 [ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有 Cookies。當(dāng)然,你也可以取消默認(rèn)的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]]; //發(fā)送cookies,根據(jù)用戶的選擇,返回相應(yīng)語言。
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];
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
iOS Crash常規(guī)跟蹤方法及Bugly集成運(yùn)用詳細(xì)介紹
這篇文章主要介紹了iOS Crash常規(guī)跟蹤方法及Bugly集成運(yùn)用詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10
iOS開發(fā)中最基本的位置功能實(shí)現(xiàn)示例
這篇文章主要介紹了iOS開發(fā)中最基本的位置功能實(shí)現(xiàn)示例,需要的朋友可以參考下2015-09-09
解析iOS開發(fā)中的FirstResponder第一響應(yīng)對(duì)象
這篇文章主要介紹了解析iOS開發(fā)中的FirstResponder第一響應(yīng)對(duì)象,包括View的FirstResponder的釋放問題,需要的朋友可以參考下2015-10-10
iOS中如何使用iconfont圖標(biāo)實(shí)例詳解
iconfont大家在開發(fā)中應(yīng)該會(huì)經(jīng)常用到,下面這篇文章主要給大家介紹了在iOS中如何使用iconfont圖標(biāo)實(shí)例的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS版微信朋友圈識(shí)別圖片位置信息 如何實(shí)現(xiàn)?
這篇文章主要為大家詳細(xì)介紹了iOS版微信朋友圈識(shí)別圖片位置信息的實(shí)現(xiàn)方法2016-10-10
iOS開發(fā)狀態(tài)欄及設(shè)置功能全面詳解
這篇文章主要為大家介紹了iOS開發(fā)狀態(tài)欄及設(shè)置功能全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

