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];