2015-04-02 89 views
0

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

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

但NSURLConnection的给这个,

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

(有两种PHPSESSIDs)

这是我的代码,

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?

回答

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