我有一个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];
我最终在这里试图找出最新情况,所以任何帮助都会非常感激。
答案 0 :(得分:0)
面临同样的问题。解析Set-Cookie标头的响应标头,手动提取cookie,并在后续请求中将其作为Cookie:
标头传回。
如果这只是最简单的会话,您不必担心持久性,HTTPS,域/路径等。
编辑:我找到了NSHTTPCookieStorage课程。看看它是否有帮助...