问题
I'm trying to make this request in an iOS app

I've already logged into the website using NSURLConnection
, the following code is in the completion handler:
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);
}
}];
The response I get in my app is something like:
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";
} }
Which, as you can see, is not the same as Chrome's response (most notably, the Content-Length
- I only got a length of 234 - I was hoping for something like 31189).
Digging around in the Network
section of the chrome developer tools, looking at what chrome does when I try to request the data, is:

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
.
回答1:
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
, whereXXX
is theid
value for the row associated with the particular student in the portal pageRequest the assignments from
novi/StudentPortal/Home/LoadProfileData/Assignments
(no?_=xxxx
parameters needed; it looks likeAjaxLoad
Javascript used that to keep track of the requests, but we don't have to worry about that here)
I notice that if you skip either of those middle two steps, the request for the assignments will fail, as shown in your question. (Frankly, this makes me question some of the design choices in that system you're interfacing with, but I assume you have no say over that.) But if you do all four, you'll get the HTML associated with the assignments.
By the way, while parsing this HTML, you might be inclined to use NSXMLParser
or regular expressions, but I'd suggest you consider Hpple (see Ray Wenderlich's article on parsing HTML).
来源:https://stackoverflow.com/questions/22652279/trying-to-make-get-request-in-objc-using-nsurl