Cocoa touch 异步请求iOS 6的NSURLConnection状态代码
当前正在使用sendAsynchronous from NSURLConnection发出post和get请求,但无法在响应中获取状态代码。大多数帖子建议使用NSURLConnection及其委托方法,据我所知,它们也是异步的 我不明白委托如何将信息发送回调用方法(最终需要数据的方法)。sendAsynchronous方法具有我正在使用的回调Cocoa touch 异步请求iOS 6的NSURLConnection状态代码,cocoa-touch,ios6,http-status-code-401,nsurlconnectiondelegate,sendasynchronousrequest,Cocoa Touch,Ios6,Http Status Code 401,Nsurlconnectiondelegate,Sendasynchronousrequest,当前正在使用sendAsynchronous from NSURLConnection发出post和get请求,但无法在响应中获取状态代码。大多数帖子建议使用NSURLConnection及其委托方法,据我所知,它们也是异步的 我不明白委托如何将信息发送回调用方法(最终需要数据的方法)。sendAsynchronous方法具有我正在使用的回调 我是新手,谢谢你的帮助 当您使用sendAsynchronousRequest:queue:completionHandle方法时,我将尝试在该上下文中回
我是新手,谢谢你的帮助 当您使用
sendAsynchronousRequest:queue:completionHandle
方法时,我将尝试在该上下文中回答:
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
// This will get the NSURLResponse into NSHTTPURLResponse format
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
// This will Fetch the status code from NSHTTPURLResponse object
int responseStatusCode = [httpResponse statusCode];
//Just to make sure, it works or not
NSLog(@"Status Code :: %d", responseStatusCode);
}];
当您使用
sendAsynchronousRequest:queue:completionHandle
方法时,我将尝试在该上下文中回答:
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
// This will get the NSURLResponse into NSHTTPURLResponse format
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
// This will Fetch the status code from NSHTTPURLResponse object
int responseStatusCode = [httpResponse statusCode];
//Just to make sure, it works or not
NSLog(@"Status Code :: %d", responseStatusCode);
}];
我有一个包装器类,它使这变得非常简单,并保持代码干净:
我有一个包装器类,它使这项工作变得非常简单,并保持代码整洁: