尝试使用NSURL在objC中进行GET请求 [英] Trying to make GET request in objC using NSURL

查看:81
本文介绍了尝试使用NSURL在objC中进行GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在iOS应用中发出此请求



我已经使用 NSURLConnection 登录了网站,以下代码位于完成处理程序中:

  NSMutableURLRequest * requestGrades = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@https://mistar.oakland.k12.mi.us/novi/ StudentPortal /主页/ LoadProfileData /分配_ = 1395809728907\" ]]; 
[requestGrades setHTTPMethod:@GET];
[NSURLConnection sendAsynchronousRequest:requestGrades queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * gradeResponse,NSData * gradeData,NSError * gradeError){

if([gradeData length]> 0& amp ;& gradeError == nil){
NSLog(@%@,requestGrades);
NSLog(@grade Response =%@ \\\
grade 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} {状态码:200,标题{
Cache-Control=公共,最大年龄= 0;
Connection =Keep-Alive;
Content-Length= 234;
Content-Type=text / html; charset = utf-8;
日期=星期三,2014年3月26日05:21:52 GMT;
过期=星期三,2014年3月26日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这样的东西)。



挖掘chrome开发人员工具的 Network 部分,查看chrome在尝试请求数据时的行为:


获取一些数据,使2 GET 请求(两张图片)。第一个获取我想要为我的应用获得的数据,第二个获得的数据看起来好像没有做任何事情。但是第二个chrome请求得到的响应与我在尝试从iOS创建第一个chrome请求时获得的响应相同(第一张图片应返回 Content-Length

所以我不确定我在做什么错我的 NSURLConnection

解决方案

做一点测试,我已经证实您必须执行以下四个 NSURLConnection NSURLSession 请求:
$ b


  • 登录

  • 转至门户网站 / novi / StudentPortal / Home / PortalMainPage

  • 设置学生横幅 / novi / StudentPortal / StudentBanner / SetStudentBanner / XXX ,其中 XXX 是与特定学生关联的行的 id 值在门户页面


  • novi / StudentPo请求分配rtal / Home / LoadProfileData / Assignments (no ?_ = xxxx 需要的参数;它看起来像 AjaxLoad Javascript用来跟踪请求,但我们不必担心这里)



我注意到,如果跳过这两个中间步骤中的任何一个,那么分配的请求将失败,如问题中所示。 (坦率地说,这让我质疑你正在接口的系统中的一些设计选择,但我认为你没有发言权。)但是,如果你全部四个,你会得到与作业相关的HTML。顺便说一句,在解析这个HTML时,你可能倾向于使用 NSXMLParser 或者正则表达式,但是我建议你考虑Hpple(请参阅Ray Wenderlich的关于解析HTML )。


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.

解决方案

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

  • Request the assignments from novi/StudentPortal/Home/LoadProfileData/Assignments (no ?_=xxxx parameters needed; it looks like AjaxLoad 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).

这篇关于尝试使用NSURL在objC中进行GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆