1

我正在学习使用 AFNetworking 并想提交登录表单。

我的代码:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:    @"mymail@yahoo.com",@"login[username]", @"123456", @"login[password]", nil];    
NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:@"index.php/customer/account/loginPost/" parameters:params];
[request setValue:@"foo=bar" forHTTPHeaderField:@"Cookie"];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
                                     {
                                         NSLog(@"oauthAuthorize Success: %@",    [operation.response allHeaderFields]);

                                     }
                                                                  failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                     {
                                         NSLog(@"oauthAuthorize Fail: %@",operation.responseString); 
                                     }];
[self enqueueHTTPRequestOperation:operation];

和我收到的标题:

    "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    Connection = "Keep-Alive";
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Wed, 28 Nov 2012 09:57:03 GMT";
    Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
    "Keep-Alive" = "timeout=5, max=98";
    "Login-Required" = true;
    Pragma = "no-cache";
    Server = "Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7";
    "Set-Cookie" = "frontend=sq26g7l2j45itg9sk6etf249r7; expires=Wed, 28-Nov-2012 10:57:04 GMT; path=/magento_1702; domain=myhost; HttpOnly";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "PHP/5.4.7";

我知道我登录失败,因为在浏览器上登录时,如果成功,它将重定向到帐户页面。

我在 MAC 上使用 bash 进行了这种形式的测试操作并得到了结果:

命令:

 curl -v  -F login[username]=mymai@yahoo.com -F login[password]=123456 --header "Cookie:foo=bar" myhost/mypage/index.php/customer/account/loginPost/

结果:

    < Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
    < X-Powered-By: PHP/5.4.7
    < Set-Cookie: frontend=ui19hig1g1pu43kdsam43naqr6; expires=Wed, 28-Nov-2012 10:46:21 GMT; path=/magento_1702; domain=myhost; HttpOnly
    < 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: persistent_shopping_cart=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/magento_1702; domain=192.168.1.254; httponly
    < Set-Cookie: frontend=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/magento_1702; domain=192.168.1.254; httponly
    < Set-Cookie: frontend=aaqja98lg13v6pc1qgema98i40; expires=Wed, 28-Nov-2012 10:46:22 GMT; path=/magento_1702; domain=192.168.1.254; HttpOnly
    < Location: http://myhost/magento_1702/index.php/customer/account/
    < Content-Length: 0
    < Content-Type: text/html; charset=UTF-8

标头的 Location 字段是帐户页面的 url,并且没有 Login-Required 字段。我认为它登录成功。

请解释一下如何使用 AFNetwoking 像使用 Bash 命令一样登录成功?

4

1 回答 1

0

我解决了我的问题。使用 AFNetworking 时,完成块被服务器重定向后调用。所以你应该在重定向块中捕捉到像 curl 命令这样的响应:

// AFHTTPRequestOperation *operation = ...;
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        // first response will come here
    }];
// ...
于 2013-03-26T09:45:42.870 回答