我正在尝试在iOS应用中提出此请求
我已经使用登录了网站NSURLConnection
,以下代码位于完成处理程序中:
NSMutableURLRequest *requestGrades = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/LoadProfileData/Assignments?_=1395809728907"]];
[requestGrades setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:requestGrades queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *gradeResponse, NSData *gradeData, NSError *gradeError) {
if ([gradeData length] > 0 && gradeError == nil) {
NSLog(@"%@",requestGrades);
NSLog(@"grade Response = %@ \ngrade Data = %@", gradeResponse, [[NSString alloc] initWithData:gradeData encoding:NSUTF8StringEncoding]);
} else {
NSLog(@"grade Error = %@", gradeError);
}
}];
我在应用程序中得到的响应类似于:
grade Response = <NSHTTPURLResponse: 0x109627500> { URL: https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/LoadProfileData/Assignments?_=1395809728907 } { status code: 200, headers {
"Cache-Control" = "public, max-age=0";
Connection = "Keep-Alive";
"Content-Length" = 234;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 26 Mar 2014 05:21:52 GMT";
Expires = "Wed, 26 Mar 2014 05:21:52 GMT";
"Last-Modified" = "Wed, 26 Mar 2014 05:21:52 GMT";
Server = "Microsoft-IIS/7.5";
Vary = "*";
"X-AspNetMvc-Version" = "4.0";
"X-Powered-By" = "ASP.NET";
} }
如您所见,它与Chrome的响应不同(最明显的是Content-Length
-我只有234的长度-我希望能得到31189之类的东西)。
Network
在chrome开发人员工具的部分中进行浏览,看看当我尝试请求数据时chrome的作用是:
Chrome, when I click the button to fetch some data, makes 2 GET
requests (the two pictures). The first one gets the data I'm trying to get for my app and the second which seems as if it doesn't really do anything. BUT the response the second chrome request is getting is the same response that I get when I try to make the first chrome request from iOS (the first picture, should return a Content-Length
of 31189).
So I'm not sure what I'm doing wrong with my NSURLConnection
.
Doing a little testing, i have confirmed that you have to do the following four NSURLConnection
or NSURLSession
requests:
Login at /novi/StudentPortal/Home/Login
Go to the portal page at /novi/StudentPortal/Home/PortalMainPage
Set the student banner /novi/StudentPortal/StudentBanner/SetStudentBanner/XXX
, where XXX
is the id
value for the row associated with the particular student in the portal page
从中请求分配novi/StudentPortal/Home/LoadProfileData/Assignments
(不需要?_=xxxx
参数;看起来像AjaxLoad
Javascript用来跟踪请求,但是我们不必为此担心)
我注意到,如果您跳过这两个中间步骤之一,则分配请求将失败,如您的问题所示。(坦率地说,这使我对与之交互的系统中的一些设计选择提出疑问,但我认为您对此没有发言权。)但是,如果您全部执行这四个操作,则将获得与分配相关联的HTML。
顺便说一下,在解析此HTML时,您可能倾向于使用NSXMLParser
正则表达式,但我建议您考虑使用Hpple(请参阅Ray Wenderlich的有关解析HTML的文章)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句