Hi All,
I am trying to do two actions in the same session.
- First I read a website to get the session secret.
- 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");
}