【发布时间】:2014-03-21 18:52:02
【问题描述】:
基本上,我正在尝试登录这个website called Mistar。我可以用 php 做到这一点:
<form action="https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login" method="post">
<input type=text name="Pin">
<input type=password name="Password">
<input type="submit" id="LoginButton">
</form>
所以 php(当你运行它时)可以工作。您使用 Pin (20005012) 和密码 (wildcats) 进行身份验证并返回带有 {1, User Authenticated} 之类的页面
现在我要做的是从 iPhone 应用程序(所以 ObjC)登录网站,可能使用 NSURLSession 或其他东西。
这是我目前所拥有的,但它一直给我一个登录页面错误:
- (void)loginToMistar {
//Create POST request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//Create and send request
NSURL *url = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSString *postString = [NSString stringWithFormat:@"<input type=text name='Pin'> <input type=password name='Password'> <input type='submit' id='LoginButton'>"];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:postBody];
// [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSString* url=[NSString stringWithFormat:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// do whatever with the data...and errors
if ([data length] > 0 && error == nil) {
NSString *loggedInPage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(loggedInPage);
}
else {
NSLog(@"error");
}
}];
谁能告诉我代码中的问题出在哪里?
【问题讨论】: