NSURLConnection返回多个php会话ID

时间:2015-04-02 05:38:49

标签: ios objective-c iphone session session-cookies

我已经使用NSURLConnection连接到Web服务(其余)并获取响应和cookie以供稍后使用。 但NSURLConnection返回多个php会话ID。这应该是错误的,因为当我使用REST Easy(firefox rest app)测试它时,它会给出以下输出。

Set-Cookie  PHPSESSID=1hk82sac38si2e58l2vvcduqh2; path=/; HttpOnly

但是NSURLConnection给出了这个,

@"PHPSESSID=f5p9ihq38sg4aeq3a98374s074; path=/; HttpOnly, PHPSESSID=e73ru08mtaf22t9rup33qjmkj5; path=/; HttpOnly, Loggedin=True;

(有两个PHPSESSID)

这是我的代码,

NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                   timeoutInterval:120.0];

request.HTTPMethod = @"POST";
NSString *stringData = [NSString stringWithFormat:@"username=%@&password=%@&grant_type=%@&client_id=%@", username, password, grantType, clientId];
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

我使用所有缓存策略值检查了这一点。但输出是一样的。

NSURLRequestUseProtocolCachePolicy = 0, 
NSURLRequestReloadIgnoringLocalCacheData = 1, 
NSURLRequestReturnCacheDataElseLoad = 2, 
NSURLRequestReturnCacheDataDontLoad = 3,

如何获得正确的php会话ID?

1 个答案:

答案 0 :(得分:0)

使用此代码段 -

NSMutableURLRequest *request = nil;
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&grant_type=%@&client_id=%@", username, password, grantType, clientId];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval: 120];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];