0

我有一个 iphone 应用程序,我正试图与 rails 后端交谈。我正在使用 NSMutableURLRequest 来回拉取数据。所有调用在 GET 请求上都可以正常工作,但是当我需要发布数据时,我的 rails 应用程序似乎无法找到会话。我已经为 get 和 POST 请求发布了下面的代码。

这是 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"];

获取请求是:

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

我在这里无所适从,试图找出发生了什么,所以任何帮助都将不胜感激。

4

1 回答 1

0

面临同样的问题。解析 Set-Cookie 标头的响应标头,手动提取 cookie,将它们作为Cookie:标头传递回后续请求。

如果这只是最简单的会话,您不必担心持久性、HTTPS、域/路径等。

编辑:我找到了 NSHTTPCookieStorage 类。看看能不能帮到...

于 2011-07-21T15:42:00.270 回答