AFNetworking sumit html表单

时间:2012-11-28 10:34:02

标签: php iphone ios magento

我正在学习使用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并且有结果:

COMAND:

 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

标题的位置字段是帐户页面的网址,并且没有登录必填字段。我认为它登录成功。

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

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。使用AFNetworking时,完成的块在服务器全部重定向后调用。所以你应该捕获重定向块以获得响应,如curl命令:

// AFHTTPRequestOperation *operation = ...;
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        // first response will come here
    }];
// ...