1

我正在尝试通过自动登录到该网站来在 UIWebView 中加载网站。我正在发布用户名和密码。它加载网站但未登录。登录不起作用。任何想法?

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

2 回答 2

1

application/x-www-form-urlencoded是否会因为在and之间缺少分号而失败charset=utf-8

试试看application/x-www-form-urlencoded;charset=UTF-8是否有帮助。

于 2013-02-20T15:33:10.937 回答
0

我刚刚用我的代码构建了一个示例应用程序来加载 webview。我在表单提交中调用了一个特定页面,它可以登录到 Web 服务。我在这里发布代码:

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

也许这段代码会对你有所帮助。

于 2013-02-20T13:32:02.480 回答