简体   繁体   English

iOS / Objective-C - 如何在不丢失初始会话的情况下发送多个NSURLSessionDataTask请求?

[英]iOS/Objective-C - How do I send multiple NSURLSessionDataTask requests without losing initial session?

I am currently working on a project in an iOS programming course. 我目前正在从事iOS编程课程的项目。 As part of a class project I am working to make a particular portion work successfully. 作为课程项目的一部分,我正在努力使特定部分成功运作。

I am using an NSAlert dialog to supply my login and password to fetch my enrolled classes in an HTML string. 我正在使用NSAlert对话框提供我的登录名和密码,以便在HTML字符串中获取已注册的类。

My college maintains students' enrolled class schedule on the college information system from which I can log in and see my classes. 我的大学维持学生在大学信息系统上的入学课程表,我可以从中登录并查看我的课程。

I want to log in to the college webpage that has a page called submit.asp to process POST requests sent from a login form. 我想登录大学网页,该网页有一个名为submit.asp的页面来处理从登录表单发送的POST请求。 On an actual browser, when I submit my credentials, and after some the server processes and redirects to another page I am able to navigate to my class schedule page with no problem. 在实际的浏览器上,当我提交我的凭据,并在一些服务器处理并重定向到另一个页面后,我能够导航到我的课程安排页面,没有任何问题。

- (BOOL) loginToStudentPortal: (NSString *) ID : (NSString *) pin
{
    NSString *submitUrl = @"https://eweb4.laccd.edu/Common/submit.asp";
    NSString *scheduleUrl = @"https://eweb4.laccd.edu/WebStudent/validate.asp";

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSURL *URL = [NSURL URLWithString:submitUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];

    NSString * submitParams = [NSString stringWithFormat:@"ssn1=%@&ssn2=&pin=%@REDIRECT/WebStudent/validate.asp", ID, pin];
    NSString * submitParams2 = [NSString stringWithFormat:@"HOLDER=Y"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[submitParams dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
           NSLog(@"%@", error);
        } else {
           //Here I Try to initiate a second dataTaskWithRequest to the next URL
           //But there is no way to jump to the destination page without losing the session

           //NSString *receivedData = [[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding];
           //NSLog(@"%@ %@", response, receivedData);

    }];
    [dataTask resume];
    return YES;
}

However, when I run the initial request from the iPhone simulator in XCode 5, I can not get the session preserved after the first request. 但是,当我在XCode 5中运行iPhone模拟器的初始请求时,我无法在第一次请求后保留会话。

Is it correct to attempt to initiate a follow on dataTask request in the else block of the main completionHandler? 尝试在main completionHandler的else块中启动后续dataTask请求是否正确?

Secondly, do I need to do make sure manually that I follow any redirects from the server? 其次,我是否需要手动确保我遵循服务器的任何重定向?

I was able to resolve the issue and finally get to the destination page. 我能够解决问题,最后到达目的地页面。

My initial attempt was a start, but I continued to investigate the behavior further and it seems that one reason I was not able to proceed was that I was debugging and I think the timeout was occurring. 我最初的尝试是一个开始,但我继续进一步调查行为,似乎我无法继续的一个原因是我正在调试,我认为超时正在发生。

When I ran the program and removed all breakpoints the program would output the correct HTML. 当我运行程序并删除所有断点时,程序将输出正确的HTML。

More importantly, I made sure that the exact chain of events was reproduced in my function. 更重要的是,我确保在我的函数中重现了确切的事件链。 I added all the dataTasks required within the completion handlers. 我添加了完成处理程序中所需的所有dataTasks。

I was eventually able to retrieve the HTML string containing the desired information. 我最终能够检索包含所需信息的HTML字符串。

It turns out the session is managed by the method I was using. 事实证明会话是由我使用的方法管理的。 Probably I had a session timeout which is why I posed the initial question. 可能我有一个会话超时,这就是为什么我提出了最初的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM