0

我无法使用 PHP 堆栈上的 REST API 进行身份验证。允许登录在浏览器上工作的查询字符串看起来像http://foo.com/gamer/index.php?module=Login&action=myLogin ...我遵循了 RestKit 上的所有可用文档并想出了以下代码,除了启用 NSLogConfigure 之外,它的输出跟在代码后面。代码

- (void)sendHTTPAuthRequestWithParams {
NSMutableDictionary* authParams = [[NSMutableDictionary alloc] init];
NSMutableDictionary* moreParams = [[NSMutableDictionary alloc] init];

//user and password params
[authParams setObject:usernameTextField.text forKey:@"login"];
[authParams setObject:passwordTextField.text forKey:@"password"];

//more parameters for rest login api
[moreParams setObject:@"Login" forKey:@"module"];
[moreParams setObject:@"mylogin" forKey:@"action"];
[moreParams setObject:authParams forKey:@"params"];

//parsing
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
NSString *json = [parser stringFromObject:moreParams error:&error];

[RKClient sharedClient].authenticationType = RKRequestAuthenticationTypeHTTP;

//if no error
if(!error) {        
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
    [[RKClient sharedClient] post:@"/index.php?" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self];  
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
}    
//[[RKClient sharedClient] post:@"/index.php?module=Login&action=mylogin" params:authParams delegate:self];   }

RKLogConfigureByName() 输出

    2012-05-24 19:59:09.513 GiantsGamer[57045:207] D restkit.network:RKRequest.m:435 Sending asynchronous POST request to URL http://www.foo.com/gamer/index.php. 
2012-05-24 19:59:09.514 GiantsGamer[57045:207] T restkit.network:RKRequest.m:381 Prepared POST URLRequest '<NSMutableURLRequest http://localhost/gamer/index.php>'. HTTP Headers: {
"Content-Length" = 85; "Content-Type" = "application/json";}. HTTP Body: {"module":"Login","action":"mylogin","params":{"login":"admin","password":"admin"}}. 2012-05-24 19:59:09.736 GiantsGamer[57045:207] D restkit.network:RKResponse.m:195 NSHTTPURLResponse Status Code: 200 2012-05-24 19:59:09.737 GiantsGamer[57045:207] D restkit.network:RKResponse.m:196 Headers: {"Cache-Control" = "no-store, must-revalidate";
    Connection = "Keep-Alive";
    "Content-Length" = 2745;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Fri, 25 May 2012 02:59:09 GMT";
    Expires = "";
    "Keep-Alive" = "timeout=5, max=100";
    Pragma = "";
    Server = "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8";
    "Set-Cookie" = "PIWIK_SESSID=a065dba239819175689b1e2a23021d01; path=/; HttpOnly";
    "X-Frame-Options" = sameorigin;
    "X-Powered-By" = "PHP/5.3.8";
}
2012-05-24 19:59:09.737 GiantsGamer[57045:207] T restkit.network:RKResponse.m:203 Read response body: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>…
</head>
<body>...
</body>
</html>

从日志中可以看出,查询字符串(认证参数)包含在 HTTP 正文中。可以吗?此外,我确实得到了 http 响应状态代码 200(OK),但随后我在 index.html 处看到了网页生成的整个 HTML 代码。不知道我哪里出错了?如何破译 RestKit 响应?

4

1 回答 1

1

您的代码不起作用,因为您将模块和操作作为 POST 值而不是 GET 发送。您可能会收到相同的 html 代码,就好像您会在没有参数的情况下向 index.php 发出请求一样。如果您愿意,您可以尝试您的代码并捕获整个 HTML 响应,并将其与网络上的 index.php HTML 响应进行比较,无需参数,我敢打赌它们是相同的。

我没有测试过这段代码,但试一试:

- (void)sendHTTPAuthRequestWithParams {

    NSString *loginPath = [NSString stringWithFormat:@"index.php?module=%@&action=%@",@"Login",@"myModule"];

    NSMutableDictionary* authParams = [[NSMutableDictionary alloc] init];
    [authParams setObject:usernameTextField.text forKey:@"login"];
    [authParams setObject:passwordTextField.text forKey:@"password"];

    [[RKClient sharedClient] post:loginPath usingBlock:^(RKRequest *request) {
        request.authenticationType = RKRequestAuthenticationTypeHTTP;
        request.method = RKRequestMethodPOST;
        request.params = authParams;
        request.delegate = self;
        request.onDidLoadResponse = ^(RKResponse *response){
            if (response.statusCode > 299) {
                //We got an error!
            }else {
                //Check Response Body to get Data!
            }
        };

    }];
}

在响应正文中,您将获得一个 NSData,如果您希望响应中有一个 json,您可以使用以下命令将其转换为 NSDictionary:NSData to NSDictionary from JSON

RK 的主要缺点是它缺乏好的文档,这里有一些你可以了解一些基础知识的东西:

您可以看到另一种更高级的方式,使用 RKObjectMapping 和 RestKit 做您想做的事情:使用 RKParams向 API 发布帖子,并使用 RestKit 使用 RKObjectMapping 映射响应

最后,您还可以检查此代码,它描述了如何使用 RESTKit 执行简单的 json POST

你也可以在 twitter 上询问 RestKit 的开发者。这对我很有用,他们回答得非常快!

于 2012-05-28T06:42:26.747 回答