2011-07-21 133 views
0

我有一個iPhone應用程序,我試圖去談談一個軌道後端。我使用NSMutableURLRequest來回拉取數據。所有的調用都可以很好地處理GET請求,但是當我需要發佈數據時,我的rails應用程序似乎無法找到會話。我已經爲get和POST請求發佈了下面的代碼。NSMutableURLRequest丟失會話信息

這是POST請求:

//Set up the URL 
NSString *url_string = [NSString stringWithFormat:@"https://testserver.example.com/players.xml"]; 
NSURL *url = [NSURL URLWithString:url_string]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:30]; 

//To create an object in rails 
//We have to use a post request 
////This is the format when using xml 
NSString *requestString = [[NSString alloc] initWithFormat:@"<player><team_game_id>%@</team_game_id><person_id>%@</person_id></player>", game, person]; 


NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:requestData]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

//For some reason rails will not take application/xml 
[request setValue:@"application/xml" forHTTPHeaderField:@"content-type"]; 

GET請求是:

NSString *url_string = [NSString stringWithFormat:@"https://testserver.example.com/people/find_by_passport?passport=%i", passport]; 
passportString = [[NSMutableString alloc] initWithFormat:@"%i", passport]; 
NSLog(@"The passport string is %@", passportString); 

NSLog(url_string, nil); 
NSURL *url = [NSURL URLWithString:url_string]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:30]; 

我在我束手無策這裏試圖找出什麼是繼續下去,因此任何幫助,將不勝感激。

回答

0

面臨同樣的問題。解析Set-Cookie頭的響應頭,手工提取cookie,並在後續請求中將頭傳回給Cookie:頭。

如果這只是最簡單的會話,您不必擔心持久性,HTTPS,域/路徑等。

編輯:我發現類NSHTTPCookieStorage。看看它是否有幫助...

+0

所以我有點明白我想在這裏做什麼,但我覺得我錯過了一些東西,我把Set-Cookie放到一個字符串中,存儲在一個鑰匙鏈項目中,在下一個請求中選擇它並將其添加到http標頭字段中。這就是我需要做的一切,通過「手工提取餅乾」你做了什麼? – drapergeek

+0

非常。另請參閱編輯。 –

+0

其實我找到了同樣的東西,我嘗試將cookie添加回頭部,但它似乎沒有幫助我。 – drapergeek