Programmatically logging in to website with saved username and password

丶灬走出姿态 提交于 2019-11-30 18:25:17

问题


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 a UIWebView?

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 the UIWebView?


回答1:


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];


来源:https://stackoverflow.com/questions/13869418/programmatically-logging-in-to-website-with-saved-username-and-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!