温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IOS 网络请求中设置cookie

发布时间:2020-10-10 09:01:52 来源:脚本之家 阅读:311 作者:lqh 栏目:移动开发

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI