In the same NSURLSession two actions

Hi All,


I am trying to do two actions in the same session.

  1. First I read a website to get the session secret.
  2. Then I use the secret in combination of username password to do a POST.


I have already make some code but I notice that it for the website two different sessions.

Because when I log the secret of both request I get different secrets back.

I am currenly only testing and figuring out (I am new with Apple Development)

My main code is : <url> is orginal the test url


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Added Session Configuration
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    //NSError *error;
    NSString *Url = @"<url>";
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
    [[session dataTaskWithURL:[NSURL URLWithString:Url]
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
                //NSString *someString = [NSString stringWithFormat:@"%@", data];
                NSString *someString = [NSString stringWithUTF8String:[data bytes]];
                NSLog(@"Start first session");
                // NSLog(@"%@", someString);
               
                NSRange startRange = [someString rangeOfString:@"<input type=\"hidden\" name=\"secret\" value=\""];
                NSRange endRange = [someString rangeOfString:@"\"><input type=\"text\" name=\"username\""];
               
                NSRange searchRange = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
                if (searchRange.location == NSNotFound)
                {
                    NSLog(@"SearchRange not Found");
                } else {
                    NSString *secretString = [someString substringWithRange:searchRange];
                    NSLog(@"%@", secretString);
                }
                NSLog(@"End first session");
               
            }]  resume];
    [[session dataTaskWithURL:[NSURL URLWithString:Url]
            completionHandler:^(NSData *data2, NSURLResponse *response2, NSError *error2){
                NSString *someString2 = [NSString stringWithUTF8String:[data2 bytes]];
                NSLog(@"START Second session");
                //NSLog(@"%@", someString2);
               
                NSRange startRange = [someString2 rangeOfString:@"<input type=\"hidden\" name=\"secret\" value=\""];
                NSRange endRange = [someString2 rangeOfString:@"\"><input type=\"text\" name=\"username\""];
               
                NSRange searchRange = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
                if (searchRange.location == NSNotFound)
                {
                    NSLog(@"SearchRange not Found");
                } else {
                    NSString *secretString2 = [someString2 substringWithRange:searchRange];
                    NSLog(@"%@", secretString2);
                }
                NSLog(@"END Second session");
            }]
     resume];
NSLog(@"THE END");
}

First up, I've moved your question over to Core OS > Networking because it's more about networking than it is about Objective-C.

Second, it's hard to debug the problem you're seeing because it's the server that controls how sessions get allocated, and presumably the server isn't running on Apple hardware. My general advice in situations like this is to do something like this:

  1. do the action in Safari

  2. do the action in your app

  3. in both cases, take a packet trace

  4. compare the traces to see what's different

  5. modify your app to be more like Safari

  6. repeat until things work

For HTTP you can use a standard packet trace; see QA1176 Getting a Packet Trace for details.

If it's HTTPS, you might want to look at a CFNetwork diagnostics log. See QA1887 CFNetwork Diagnostic Logging. In that case you won't be able to use Safari (because you can't set the environment variable in Safari) but you could do the same thing with a UIWebView running inside a small test app.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
In the same NSURLSession two actions
 
 
Q