Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSMutableURLRequest丢失会话信息_Iphone_Ios_Session_Nsurlrequest_Nsmutableurlrequest - Fatal编程技术网

Iphone NSMutableURLRequest丢失会话信息

Iphone NSMutableURLRequest丢失会话信息,iphone,ios,session,nsurlrequest,nsmutableurlrequest,Iphone,Ios,Session,Nsurlrequest,Nsmutableurlrequest,我有一个iphone应用程序,我正试图与rails后端对话。我使用NSMutableURLRequest来回拉取数据。所有的调用在GET请求上都可以正常工作,但是当我需要发布数据时,我的rails应用程序似乎找不到会话。我在下面发布了get和POST请求的代码 这是POST请求: //Set up the URL NSString *url_string = [NSString stringWithFormat:@"https://testserver.example.com/players.x

我有一个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"];

我在这里绞尽脑汁想弄清楚到底发生了什么事,因此非常感谢您的帮助。

面临同样的问题。解析设置Cookie头的响应头,手动提取Cookie,在后续请求中将其作为
Cookie:
头传递回

如果这只是简单的会话,那么您就不必担心持久性、HTTPS、域/路径等问题


编辑:我找到了NSHTTPCookieStorage类。看看它是否有帮助…

所以我有些理解我在这里要做什么,但我觉得我遗漏了什么,我把设置好的Cookie拉到一个字符串中,存储在一个键链项目中,在下一个请求时将其重新提取,并将其添加到http头字段中。这就是我所需要做的吗,你认为“手工提取饼干”是什么意思?事实上,我发现了同样的东西,我尝试将饼干添加回标题中,但它似乎对我没有任何帮助。。
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];