IOS 網路請求中設定cookie

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   strong   檔案   

本文轉載至 http://tec.5lulu.com/detail/108k0n1e626py8s96.html  1ASIHTTPRequest

 

ASIHTTPRequest 是一款極其強勁的 HTTP 訪問開源項目。讓簡單的 API 完成複雜的功能,如:非同步請求,隊列請求,GZIP 壓縮,緩衝,斷點續傳,進度跟蹤,上傳檔案,HTTP 認證。

 

 

cookie的支援

    如果 Cookie 存在的話,會把這些資訊放在 NSHTTPCookieStorage 容器中共用,並供下次使用。你可以用 [ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有 Cookies。當然,你也可以取消預設的Cookie策略,而使自訂的Cookie:

  1. -(NSMutableArray*)retrunCookies{
  2. NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
  3. [properties setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
  4. [properties setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
  5. [properties setValue:@"" forKey:NSHTTPCookieDomain];
  6. [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
  7. [properties setValue:@"" forKey:NSHTTPCookiePath];
  8. NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
  9. return [NSMutableArray arrayWithObject:cookie];
  10. }
  11. [request setRequestCookies:[self retrunCookies]]; //發送cookies,根據使用者的選擇,返回相應語言。

 

2 NSMutableURLRequest(可以用於webview)
  1. NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
  2. [properties setValue:userId forKey:NSHTTPCookieValue];
  3. [properties setValue:@"BENQGURU.GAIA.USERID" forKey:NSHTTPCookieName];
  4. [properties setValue:@"" forKey:NSHTTPCookieDomain];
  5. [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
  6. [properties setValue:@"/" forKey:NSHTTPCookiePath];
  7. NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
  8. NSDictionary *properties1 = [[[NSMutableDictionary alloc] init] autorelease];
  9. [properties1 setValue:[LoginViewController getLanguageType:loginInfo.lang] forKey:NSHTTPCookieValue];
  10. [properties1 setValue:@"BENGGURU.GAIA.CULTURE_CODE" forKey:NSHTTPCookieName];
  11. [properties1 setValue:@"" forKey:NSHTTPCookieDomain];
  12. [properties1 setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
  13. [properties1 setValue:@"/" forKey:NSHTTPCookiePath];
  14. NSHTTPCookie *cookie1 = [[[NSHTTPCookie alloc] initWithProperties:properties1] autorelease];
  15. NSArray *cookies=[NSArray arrayWithObjects:cookie,cookie1,nil];
  16. NSDictionary *headers=[NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
  17. NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[object valueForKey:@"url"]]];
  18. [request setValue:[headers objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
  19. [webView loadRequest:request];

IOS 網路請求中設定cookie

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.