使用保存的用户名和密码以编程方式登录网站 [英] Programmatically logging in to website with saved username and password
问题描述
我正在制作的一个应用程序的功能之一是将用户登录到我们的校园门户网站。为了便于使用,用户可以在应用程序中输入用户名和密码一次,并将其保存用于所有未来的登录。当用户点击一个按钮时,会自动发送信息以登录,并且该网站将显示在 UIWebView
中。
让我们说,用户名和密码都存储在 NSString
中。我如何使用此数据以编程方式登录此网站并显示它在一个 UIWebView
?
我对发布和表单知之甚少, p>
编辑:
感谢Karthik,我能够使用Firebug找到HTTP Post。它给了我:
appName = ridgefield& portalUrl = portal%2Fridgefield.jsp%3F%26rID%3D0.18423783694092& username = < username>& password =< password>& B1 = Log + In& url = portal%2Fmain.xsl%3FrID%3D0.6845596700302482& lang = en
pre>
其中代表真正的用户名和密码。我相信这是我需要的。使用这个,我怎样才能在
UIWebView
中显示登录页面?我只需要在UIWebView
中加载上面的URL?解决方案使用UIWebView,并执行http POST来 https://ic.ridgefield.org/campus/verify使用用户名和密码。
要了解它的工作原理,请在Firefox浏览器上安装Firebug并转到萤火虫上的'Net'选项卡,然后打开您的网站并输入一些用户名/密码。
您应该使用代码模仿该操作。 (我总是从服务器得到无效的用户名或密码响应,因为我没有帐户,我尝试了一些随机的,因为没有在网站上注册,所以我没有办法验证这一点)
UIWebView * webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
NSString * username = @;
NSString * password = @;
NSURL * url = [NSURL URLWithString:@https://ic.ridgefield.org/campus/verify.jsp];
NSString * body = [NSString stringWithFormat:@appName = ridgefield& username =%@& password =%@,username,password];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
request.HTTPMethod = @POST;
request.HTTPBody = [body dataUsingEncoding:NSStringEncodingConversionAllowLossy];
[webView loadRequest:request];
One of the functions of an app I am making involves logging the user into our Campus Portal website. For ease of use, the user may enter a username and password into the app once, and it will be saved for all future log-ins. When the user clicks a button, the information will automatically be sent to log in, and the website will be displayed in a
UIWebView
.Let us say that the username and password are each stored in an
NSString
. How can I use this data to log in to this website programmatically and display the page it in aUIWebView
?I know little about posting and forms, so any help is appreciated.
Would something like this Stackoverflow answer help?
Here's the shell of my code for this
- (IBAction)btnGo:(id)sender { username = usernameField.text; password = passwordField.text; if (saveSwitch.isOn) { //Save data if the user wants AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; usernameSaved = username; passwordSaved = password; [appDelegate.listOfPortalCredentials replaceObjectAtIndex:0 withObject:usernameSaved]; [appDelegate.listOfPortalCredentials replaceObjectAtIndex:1 withObject:passwordSaved]; } //Insert username and password to web form, log in to portal //This is where I need help }
Edit:
Thanks to Karthik I was able to find the HTTP Post using Firebug. It gave me:
appName=ridgefield&portalUrl=portal%2Fridgefield.jsp%3F%26rID%3D0.18423783694092&username=<username>&password=<password>&B1=Log+In&url=portal%2Fmain.xsl%3FrID%3D0.6845596700302482&lang=en
where and represent the real username and password. I believe this is what I need. Using this, how can I display the logged-in page in a
UIWebView
? Do I just need to load the above URL in theUIWebView
?解决方案Use UIWebView, and do a http POST to https://ic.ridgefield.org/campus/verify.jsp with username and password.
To understand how it works, install Firebug on Firefox browser and go to 'Net' tab on firebug, and then open your website and enter some username/password.
You should mimic that action using code. (I always get invalid username or password response from server, coz i dont have an account and i try some random ones, and since there is no signup on the site, there is no way for me to verify this)
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame]; [self.view addSubview:webView]; NSString* username = @""; NSString* password = @""; NSURL* url = [NSURL URLWithString:@"https://ic.ridgefield.org/campus/verify.jsp"]; NSString* body = [NSString stringWithFormat:@"appName=ridgefield&username=%@&password=%@", username, password]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30]; request.HTTPMethod = @"POST"; request.HTTPBody = [body dataUsingEncoding:NSStringEncodingConversionAllowLossy]; [webView loadRequest:request];
这篇关于使用保存的用户名和密码以编程方式登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!