IOS 網(wǎng)絡請求中設置cookie
IOS 網(wǎng)絡請求中設置cookie
1. ASIHTTPRequest
ASIHTTPRequest 是一款極其強勁的 HTTP 訪問開源項目。讓簡單的 API 完成復雜的功能,如:異步請求,隊列請求,GZIP 壓縮,緩存,斷點續(xù)傳,進度跟蹤,上傳文件,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]]; //發(fā)送cookies,根據(jù)用戶的選擇,返回相應語言。
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程序開發(fā)之使用PlaceholderImageView實現(xiàn)優(yōu)雅的圖片加載效果
這篇文章主要介紹了ioS程序開發(fā)之使用PlaceholderImageView實現(xiàn)優(yōu)雅的圖片加載效果的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09詳解iOS中UIView的layoutSubviews子視圖布局方法使用
這篇文章主要介紹了iOS中UIView的layoutSubviews子視圖布局方法使用,文中舉了一個layoutSubviews在iPad橫豎屏切換時被調(diào)用用來重新布局的實例,需要的朋友可以參考下2016-04-04iOS tableView實現(xiàn)頭部拉伸并改變導航條漸變色
這篇文章主要為大家詳細介紹了iOS tableView實現(xiàn)頭部拉伸并改變導航條漸變色,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05iOS開發(fā)WebViewJavascriptBridge通訊原理解析
這篇文章主要為大家介紹了iOS開發(fā)WebViewJavascriptBridge通訊原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11ios的collection控件的自定義布局實現(xiàn)與設計
這篇文章主要介紹了mac、iOS端支持自定義布局的collection控件的實現(xiàn)與設計,需要的朋友學習參考下吧。2017-12-12