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];
IOS 网络请求中设置cookie的更多相关文章
- 网络请求中的cookie与set-Cookie的交互模式的一些问题解析
首先我们需要思考,很多问题. 1.当很多人访问统一个网服务器,服务器如何来区分不同的用户呢? 答:sessionid,sessionid保证了浏览器和服务器唯一性的通信凭证号码,session保存在服 ...
- iOS 网络请求中的challenge
这里有一篇文章,请阅读,感谢作者!http://blog.csdn.net/kmyhy/article/details/7733619 当请求的网站有安全认证问题时,都需要通过 [[challenge ...
- iOS 网络请求中的空类型字符串转换
创建一个工具类, .h: #import <Foundation/Foundation.h> @interface MySetNullWithStrTool : NSObject +( ...
- IOS 在http请求中使用cookie
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/805173 一直以 ...
- iOS网络请求-AFNetworking源码解析
趁着端午节日,自己没有什么过多的安排,准备花4-5天左右,针对网络请求源码AFNetworking和YTKNetwork进行解析以及这两年多iOS实际开发经验(其实YTKNetwork也是对AFNet ...
- Angularjs之如何在跨域请求中传输Cookie
一般情况我们在使用WebApi之类的技术时,都会遇到跨域的问题,这个只需要在服务端做一下处理即可. 如果这些GET或POST请求不需要传递Cookie数据的话,就没什么问题了,但如果需要,那么会发现 ...
- 【转载】一步一步搭建自己的iOS网络请求库
一步一步搭建自己的iOS网络请求库(一) 大家好,我是LastDay,很久没有写博客了,这周会分享一个的HTTP请求库的编写经验. 简单的介绍 介绍一下,NSURLSession是iOS7中新的网络接 ...
- iOS网络请求之multipart/form-data提交数据
multipart/form-data表单数据 在http网络请求中,post没有请求长度的限制,因为post把数据放在了body中,而不是像Get一样放在了浏览器的地址栏中(可以这么理解), 所以相 ...
- springmvc接口ios网络请求
springmvc: application/json;charset=utf-8的ios网络请求: 后台使用 @RequestBody注解参数接收:
随机推荐
- JBOSS安全配置
1.jmx-console登录的用户名和密码设置 默认情况访问http://localhost:8080/jmx-console就可以浏览jboss的部署管理的一些信息,不需要输入用户名和密码,使用起 ...
- RPC、SQL、NFS属于OSI的哪一层
第一层:物理层 第二层:数据链路层 802.2.802.3ATM.HDLC.FRAME RELAY 第三层:网络层 IP.IPX.ARP.APPLETALK.ICMP 第四层:传输层 TCP.UDP. ...
- defer 要放在return之前
虽然defer可以在函数退出的时候执行,但是也要把它放在return之前,否则不会调用到. 错误的示范: res, err := Resource.open() if err != nil { ret ...
- Windows 8上使用SQL 2012 Configuration Manager
现在使用Windows 8 + Sql Server 2012,而今天想用SQL 2012 Configuraton Manager, 设置别名来访问不同的开发数据库.但发现没能找到SQL 2012 ...
- DG - 开启Active Data Guard
在配置active data guard之前,phycial standby必须处于以下两个状态之一: (1)standby处于mount状态,并且redo apply正在运行中 (2)standby ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- C# 微信支付教程系列之扫码支付
微信支付教程系列之扫码支付 今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添加好友的时候 ...
- FileInputStream and FileOutputStream
Java FileOutputStream class Java FileOutputStream is an output stream for writing data to a file. If ...
- [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创] 分享一些linux教程
书<鸟哥的linux私房菜第三版>,链接:http://pan.baidu.com/s/1i3femnr 配套视频,链接:http://pan.baidu.com/s/1v72xw --- ...