AFNetworking 检查响应

AFNetworking check response

如何检查是否返回响应:

HTTP/1.1 302 Moved Temporarily

Server: nginx/1.5.6

Date: Wed, 18 Mar 2015 19:23:02 GMT

Content-Type: text/html

Transfer-Encoding: chunked

Connection: keep-alive

Last-Modified: Wed, 18 Mar 2015 19:23:02 GMT

Set-Cookie: PHPSESSID=1oh16nbnn9anin3r71tcsmo804; path=/

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Pragma: no-cache

Set-Cookie: er_referer=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT

Set-Cookie: S_ID=1061662561; path=/

Location: /core/

请求代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:ua forHTTPHeaderField:@"User-Agent"];
    manager.requestSerializer = requestSerializer;
    AFHTTPResponseSerializer * responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer = responseSerializer;
    NSDictionary *parameters = @{@"login": myLogin, @"pass": myPassword};
    [manager POST:urlLogin parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([operation.responseString rangeOfString:@""].location == NSNotFound) {
            NSLog(@"Login or password false");
        } else {

如果响应html我是这样做的if ([operation.responseString rangeOfString: @ "Then I put the titel"]. Location == NSNotFound),但不知道在这种情况下该怎么做

The Foundation URL 加载系统会自动将您的请求重定向到 Location header.

中指定的路径

如果您需要拦截重定向,通常是为了处理身份验证问题,您可以在 operation object 上设置身份验证质询块(如果需要,还可以设置重定向响应块)。

Here's an AFNetworking GitHub issue 其中讨论了这个问题。如果您需要,它有一些示例代码。