문제

I'm trying to load a website in UIWebView by automatically login to that website. I'm posting username and password . It loads the website but not logged in. The login not working. any idea?

NSURL *url = [NSURL URLWithString:@"http://www.abc.com"];

        NSString *post = [NSString stringWithFormat:@"login_name=%@&login_password=%@&submit=Login", @"abc", @"abc"];
        NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        NSLog(@"POST Length = %@",postLength);
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        [request setTimeoutInterval:5.0];
         [webView loadRequest:request];
도움이 되었습니까?

해결책 2

I just built a sample app with my code to load a webview. I call a specific page in my form submission and it works to login to the web service. I am posting the code here:

NSString *username = @"abc";
NSString *password = @"abc";
NSString *postString = [NSString stringWithFormat:@"username=%@&password=%@",username, password];
NSLog(@"%@", postString);
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.someurl.com/loginscript"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[webView loadRequest:request];

Maybe this code will help you.

다른 팁

Could it be failing because you're missing a semicolon between application/x-www-form-urlencoded and charset=utf-8?

Try application/x-www-form-urlencoded;charset=UTF-8 and see if that helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top