NSMutableURLRequest 丢失会话信息

发布于 2024-11-25 05:51:38 字数 1959 浏览 2 评论 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"];

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

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

I have an iphone app that I'm trying to get to talk to a rails back end. I'm using NSMutableURLRequest to pull data back and forth. All the calls work fine on GET requests but when I need to post data, my rails app can't seem to find the session. I've posted the code below for both a get and a POST request.

This is the POST request:

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

The get request is:

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

I am at my wits end here trying to find out whats going on so any help would be GREATLY appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2024-12-02 05:51:38

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

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

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

Faced the same issue. Parse the response headers for the Set-Cookie header, extract the cookies by hand, pass them back as Cookie: header on subsequent requests.

If this is just about sessions in their simplest, you don't have to worry about persistence, HTTPS, domains/paths and such.

EDIT: I found class NSHTTPCookieStorage. See if it can be of help...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文