POST 请求成功后打开第二个控制器
Posted
技术标签:
【中文标题】POST 请求成功后打开第二个控制器【英文标题】:Opening of the second controller after a successful POST request 【发布时间】:2015-03-14 11:34:34 【问题描述】:我只学习 Obj-c。我正在发送登录名和密码,返回 html 页面,如何在请求后打开页面并关闭 loginWindowController?是请求本身:
NSString *myLogin = usernameTextField.stringValue;
NSString *myPassword = passwordTextField.stringValue;
NSString *myFlash = flashpasswordTextField.stringValue;
NSString *myNid = @"123456";
NSString *urlString = @"http://neverlands.ru/game.php";
NSString *ua = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:ua forHTTPHeaderField:@"User-Agent"];
manager.requestSerializer = requestSerializer;
AFHTTPResponseSerializer * responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer = responseSerializer;
NSDictionary *parameters = @@"player_nick": myLogin, @"player_password": myPassword;
[manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
//Условия после запроса 1
if ([operation.responseString rangeOfString:@"NeverLands: Земли, которых нет... - Второй пароль"].location == NSNotFound)
NSLog(@"Неверный логин или пароль");
else
NSDictionary *parameters = @@"flcheck": myFlash, @"nid": myNid;
[manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
//Условия после запроса 2
if ([operation.responseString rangeOfString:@"NeverLands: Земли, которых нет... - бесплатная браузерная онлайн игра в стиле фэнтези"].location == NSNotFound)
NSLog(@"Неверный флеш пароль");
else
mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindow"];
[mainWindowController showWindow:self];
[mainWindowController addWebView:[NSURL URLWithString:urlString]];
NSLog(@"Авторизация произведена");
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
在这里写什么会打开当前会话?
mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindow"];
[mainWindowController showWindow:self];
[mainWindowController addWebView:[NSURL URLWithString:HERE]];
我试过[mainWindowController addWebView:[NSURL URLWithString:urlString]];
但是打开一个新会话的页面并写道没有输入用户名和密码。
【问题讨论】:
【参考方案1】:看起来您正在发出相同的发布请求,具有相同的正文,但在响应中寻找 2 个不同的内容?
您应该对响应正文进行 2 次检查
NSString *importantStuff1 = @"NeverLands: Земли, которых нет... - Второй пароль";
NSString *importantStuff2 = @"NeverLands: Земли, которых нет... - бесплатная браузерная онлайн игра в стиле фэнтези";
if (([operation.responseString rangeOfString:importantStuff1].location == NSNotFound) &&
([operation.responseString rangeOfString:importantStuff2].location == NSNotFound))
NSLog(@"Couldn't find the important stuff needed");
else
mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindow"];
[mainWindowController showWindow:self];
[mainWindowController addWebView:[NSURL URLWithString:urlString]];
NSLog(@"Авторизация произведена");
请原谅变量名我不知道这些词是什么哈哈
【讨论】:
本站两步认证。 1.登录 2.如果用户名和密码都正确则页面加载flash元素输入第二个密码neverlands.ru/swf/flashpass.swf然后发送两个请求,都成功了,通过Charles Web Proxy查看它的headers from第一次授权NSDictionary *parameters = @@"flcheck": myFlash, @"nid": myNid;
那么您当前的实现是否有错误?或者当showWindow
被调用时,它只是没有像你期望的那样显示窗口吗?
我需要在第二个请求成功打开之后 MainWindowController MainWindow.xib (这是我做的一个网络浏览器)和登录后的页面加载站点。
照顾我看到的第二个请求 charles Web 代理响应:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" id="html"> <head> <title>NeverLands: Земли, которых нет... - бесплатная браузерная онлайн игра в стиле фэнтези</title> <SCRIPT src="./js/game.js"></SCRIPT> <SCRIPT> view_frames(); </SCRIPT> </head> </html>
我如何理解需要运行这个 js 才能进入站点 neverlands.ru/js/game.js以上是关于POST 请求成功后打开第二个控制器的主要内容,如果未能解决你的问题,请参考以下文章
在 Xcode 5 中使用 NSUserDefaults 保存第二个打开的视图