AFNetworking 支持cookies的解决方案
来源:互联网 发布:淘宝网书店 编辑:程序博客网 时间:2024/06/05 11:07
AFNetworking是支持cookies,只不过它把这个逻辑交给了iOS 底层的api处理了。
多话不说了,很讨厌网上那些人云亦云的帖子,三人成虎!
本次我们的项目重构,需要用到cookies,我直接给出解决方案吧:
(cookie使用支持的简单思路是:首次请求时,服务器取回cookies,然后每次请求时附加上cookie,如此反复即可,至于cookies中有啥内容,我们不用关注,服务器要就给她)
我使用的是AFNetworking的AFHTTPClient进行网络访问的,我就直接在AFHTTPClient.h添加两个支持cookies的方法,一个是为post写的,另一个是为get写的。
1、在AFHTTPClient.h添加两个支持cookies的方法,每次请求时,都发送出本地cookies
1
2
3
4
5
// add by block cheng
- (
void
)blockGetPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(
void
(^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(
void
(^)(AFHTTPRequestOperation *operation, NSError *error))failure;
1
2
3
4
5
//add by block cheng
- (
void
)blockPostPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(
void
(^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(
void
(^)(AFHTTPRequestOperation *operation, NSError *error))failure;
其实现是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
- (
void
)blockGetPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(
void
(^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(
void
(^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
if
(!path) {
path = @
""
;
}
NSArray *arcCookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @
"sessionCookies"
]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for
(NSHTTPCookie *cookie in arcCookies){
[cookieStorage setCookie: cookie];
}
NSURL *dataUrl = [NSURL URLWithString:path relativeToURL:self.baseURL];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:dataUrl];
//id: NSHTTPCookie
NSDictionary *sheaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
__strong NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];
[request setHTTPMethod:@
"GET"
];
[request addValue:@
"iOS"
forHTTPHeaderField:@
"User-Agent"
];
[request setValue:[NSString stringWithFormat:@
"application/x-www-form-urlencoded; charset=%@"
, charset] forHTTPHeaderField:@
"Content-Type"
];
// [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, NSUTF8StringEncoding) dataUsingEncoding:NSUTF8StringEncoding]];
[request setAllHTTPHeaderFields:sheaders];
if
(parameters) {
dataUrl = [NSURL URLWithString:[[dataUrl absoluteString] stringByAppendingFormat:[path rangeOfString:@
"?"
].location == NSNotFound ? @
"?%@"
: @
"&%@"
, AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
[request setURL:dataUrl];
}
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
post实现是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
- (
void
)blockPostPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(
void
(^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(
void
(^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
if
(!path) {
path = @
""
;
}
NSArray *arcCookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @
"sessionCookies"
]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for
(NSHTTPCookie *cookie in arcCookies){
[cookieStorage setCookie: cookie];
}
NSURL *dataUrl = [NSURL URLWithString:path relativeToURL:self.baseURL];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:dataUrl];
//id: NSHTTPCookie
NSDictionary *sheaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
__strong NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f];
[request setHTTPMethod:@
"Post"
];
[request addValue:@
"iOS"
forHTTPHeaderField:@
"User-Agent"
];
[request setValue:[NSString stringWithFormat:@
"application/x-www-form-urlencoded; charset=%@"
, charset] forHTTPHeaderField:@
"Content-Type"
];
// [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, NSUTF8StringEncoding) dataUsingEncoding:NSUTF8StringEncoding]];
[request setAllHTTPHeaderFields:sheaders];
if
(parameters) {
NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
NSError *error = nil;
switch
(self.parameterEncoding) {
case
AFFormURLParameterEncoding:;
[request setValue:[NSString stringWithFormat:@
"application/x-www-form-urlencoded; charset=%@"
, charset] forHTTPHeaderField:@
"Content-Type"
];
[request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
break
;
case
AFJSONParameterEncoding:;
[request setValue:[NSString stringWithFormat:@
"application/json; charset=%@"
, charset] forHTTPHeaderField:@
"Content-Type"
];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
break
;
case
AFPropertyListParameterEncoding:;
[request setValue:[NSString stringWithFormat:@
"application/x-plist; charset=%@"
, charset] forHTTPHeaderField:@
"Content-Type"
];
[request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]];
break
;
}
if
(error) {
NSLog(@
"%@ %@: %@"
, [self
class
], NSStringFromSelector(_cmd), error);
}
}
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
对应的使用方法分别是:
2、每次请求返回时,保存cookie,以供以后使用
get的使用方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
*执行get请求,支持cookie
*TODO: 需要完善
**/
-(
void
)asynchronousCookiesGET:(NSString *)path witParams:(NSMutableDictionary *)params
{
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL_SESSION]];
self.client = httpClient;
[httpClient release];
[self.client blockGetPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
D_END;
LogNET(@
"------------------------GET--->------------------------------"
);
LogNET(@
"netWorking url:: %@"
,operation.request.URL.absoluteString);
LogNET(@
"netWorking params:: %@"
,params);
LogNET(@
"net working statuCode:: %d"
,operation.response.statusCode);
LogNET(@
"net working responseString:: %@"
,operation.responseString);
LogNET(@
"------------------------GET---<-----------------------------"
);
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for
(NSHTTPCookie *cookie in cookies) {
// Here I see the correct rails session cookie
NSLog(@
"Block cookie: %@"
, cookie);
}
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @
"sessionCookies"
];
[defaults synchronize];
//handle server return data;
}failure:^(AFHTTPRequestOperation *operation, NSError* error)
{
LogNET(@
"------------------------GET--->------------------------------"
);
LogNET(@
"netWorking url:: %@"
,operation.request.URL.absoluteString);
LogNET(@
"net working statuCode:: %d"
,operation.response.statusCode);
LogNET(@
"net working responseString:: %@"
,operation.responseString);
LogNET(@
"------------------------GET---<-----------------------------"
);
WebExceptionEntity* exception = [[WebExceptionEntity alloc] initWithExceptionString:operation.responseString withStatusCode:operation.response.statusCode withError:error];
self.webException= exception;
[exception release];
if
(self.exceptionBlock)
{
self.exceptionBlock(self,self.webException);
}
}
];
}
Post的使用方式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
*执行post请求:支持cookie的版本
**/
-(
void
)asynchronousCookiesPost:(NSString *)path
witParams:(NSMutableDictionary *)params
{
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL_SESSION]];
self.client = httpClient;
[httpClient release];
if
(self.preBlcok) {
self.preBlcok(self);
}
[self.client blockPostPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
D_END;
LogNET(@
"------------------------POST--->------------------------------"
);
LogNET(@
"netWorking url:: %@"
,operation.request.URL.absoluteString);
LogNET(@
"netWorking params:: %@"
,params);
LogNET(@
"net working statuCode:: %d"
,operation.response.statusCode);
LogNET(@
"net working responseString:: %@"
,operation.responseString);
LogNET(@
"------------------------POST---<-----------------------------"
);
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for
(NSHTTPCookie *cookie in cookies) {
// Here I see the correct rails session cookie
NSLog(@
"cookie: %@"
, cookie);
}
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @
"sessionCookies"
];
[defaults synchronize];
//handle server return data
}failure:^(AFHTTPRequestOperation *operation, NSError* error)
{
LogNET(@
"------------------------POST--->------------------------------"
);
LogNET(@
"netWorking url:: %@"
,operation.request.URL.absoluteString);
LogNET(@
"net working statuCode:: %d"
,operation.response.statusCode);
LogNET(@
"net working responseString:: %@"
,operation.responseString);
LogNET(@
"------------------------POST---<-----------------------------"
);
WebExceptionEntity* exception = [[WebExceptionEntity alloc] initWithExceptionString:operation.responseString withStatusCode:operation.response.statusCode withError:error];
self.webException= exception;
[exception release];
if
(self.exceptionBlock)
{
self.exceptionBlock(self,self.webException);
}
}];
}
测试运行:
1
2
3
4
5
6
2014-02-14 10:00:53.465 TripPlus[15245:60b] cookie: <NSHTTPCookie version:0 name:
"ssid"
value:
"79244stsh4p3shv1ftd1125d90"
expiresDate:(null) created:2014-02-14 02:00:53 +0000 (4.14036e+08) sessionOnly:TRUE domain:
"192.168.1.199"
path:
"/"
isSecure:FALSE>
//--
使用cookies
TPTripPlusDetailViewController.m:141 .... 开始请求
AFHTTPClient.m:706 post add cookie:<NSHTTPCookie version:0 name:
"acb5f57bfaec0550abdb337d5e8f0f40"
value:
"1febc174d86a8f5473af180658a7d9369b1e35daa%3A4%3A%7Bi%3A0%3Bs%3A1%3A%221%22%3Bi%3A1%3Bs%3A17%3A%22test%40joviainc.com%22%3Bi%3A2%3Bi%3A2592000%3Bi%3A3%3Ba%3A0%3A%7B%7D%7D"
expiresDate:2014-03-16 02:00:50 +0000 created:2001-01-01 00:00:01 +0000 (1) sessionOnly:FALSE domain:
"192.168.1.199"
path:
"/"
isSecure:FALSE>
AFHTTPClient.m:706 post add cookie:<NSHTTPCookie version:0 name:
"ssid"
value:
"79244stsh4p3shv1ftd1125d90"
expiresDate:(null) created:2001-01-01 00:00:01 +0000 (1) sessionOnly:TRUE domain:
"192.168.1.199"
path:
"/"
isSecure:FALSE>
从结果可看出,完美运行。
2 0
- AFNetworking 支持cookies的解决方案
- AFNetworking 支持cookies的解决方案
- AFNetworking 支持cookies的解决方案
- AFNetworking-cookies 的使用
- AFNetworking-cookies 的使用
- AFnetWorking带Cookies的请求
- AFNetworking网络请求时注意的cookies缓存问题
- AFNetworking读取和设置cookie的解决方案
- AFNetworking读取和设置cookie的解决方案
- AFNetworking读取和设置cookie的解决方案
- AFNetworking读取和设置cookie的解决方案
- 判断访问者的浏览器是否支持JavaScript和Cookies
- 判断访问者的浏览器是否支持JavaScript和Cookies
- 黑客cookies和session欺骗的工作原理和解决方案
- 乱码的解决方案——cookies中包含中文
- c# Cookies中不能包含逗号的解决方案
- iOS网络通信类库用AFNetworking替换ASHttpRequest的解决方案
- AFNetworking上传参数乱码的问题解决方案 (新增base64解决方案)
- Python中有效的字符串合并方法
- 开发一个App要100万?太扯淡
- 网络攻击
- C#Xml,json解析以及Socket传输问题
- 从Android运行时出发,打造我们的脱壳神器
- AFNetworking 支持cookies的解决方案
- android View事件传递
- Codeforces Round #326 (Div. 2) D. Duff in Beach(LIS)
- UE4 main classes explanation
- MR-2.MapReduce序列化&反序列化&MapReduce函数
- Postgresql通过批处理命令执行sql文件
- VMware中的Linux通过NAT方式和本机联网设置
- poj-2352 && HDU-1541 --Stars(树状数组)
- CentOS6.5 python+ipython安装