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

ヘッダーの[場所]フィールドはアカウントページのURLであり、[ログインが必要]フィールドはありません。ログイン成功だと思います。

Bashコマンドを使用するようにAFNetwokingを使用してログイン成功する方法を教えてください。

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 に答える