Http 具有多个参数的RestKit身份验证

Http 具有多个参数的RestKit身份验证,http,login,restkit,Http,Login,Restkit,我在使用PHP堆栈上的RESTAPI进行身份验证时遇到困难。允许登录在浏览器上工作的查询字符串类似于。。。除了启用NSLogConfigure(其输出遵循代码)之外,我还遵循了RestKit上的所有可用文档,并生成了以下代码。 代码 -(void)sendHTTPAuthRequestWithParams{ NSMutableDictionary*authParams=[[NSMutableDictionary alloc]init]; NSMutableDictionary*moreparms

我在使用PHP堆栈上的RESTAPI进行身份验证时遇到困难。允许登录在浏览器上工作的查询字符串类似于。。。除了启用NSLogConfigure(其输出遵循代码)之外,我还遵循了RestKit上的所有可用文档,并生成了以下代码。 代码

-(void)sendHTTPAuthRequestWithParams{
NSMutableDictionary*authParams=[[NSMutableDictionary alloc]init];
NSMutableDictionary*moreparms=[[NSMutableDictionary alloc]init];
//用户和密码参数
[authParams setObject:usernameTextField.text-forKey:@“登录”];
[authParams setObject:passwordTextField.text-forKey:@“password”];
//rest登录api的更多参数
[moreParams setObject:@“登录”forKey:@“模块”];
[moreParams setObject:@“mylogin”forKey:@“action”];
[moreParams setObject:authParams forKey:@“params”];
//解析
id parser=[[RKParserRegistry sharedRegistry]ParserFormetype:RKPimetypeJSON];
n错误*错误=nil;
NSString*json=[解析器stringFromObject:moreParams错误:&错误];
[RKClient sharedClient].authenticationType=RKRequestAuthenticationTypeHTTP;
//如果没有错误
如果(!error){
RKLogConfigureByName(“RestKit/Network”,RKLogLevelTrace);
[[RKClient sharedClient]post:@/index.php?”参数:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding]MIMEType:RKMIMETypeJSON]委托:self];
RKLogConfigureByName(“RestKit/Network”,RKLogLevelTrace);
}    
//[[RKClient sharedClient]post:@/index.php?module=Login&action=mylogin“params:authParams委托:self];}
RKLogConfigureByName()输出

2012-05-2419:59:09.513 GiantsGamer[57045:207]D restkit.网络:RKRequest.m:435向URL发送异步POST请求http://www.foo.com/gamer/index.php. 
2012-05-24 19:59:09.514 GiantsGamer[57045:207]T restkit.网络:RKRequest.m:381准备好了URLRequest后的文件“”。HTTP标头:{
“内容长度”=85;“内容类型”=“应用程序/json”;}。HTTP正文:{“模块”:“登录”,“操作”:“mylogin”,“参数”:{“登录”:“管理员”,“密码”:“管理员”}}。2012-05-2419:59:09.736 GiantsGamer[57045:207]D restkit.网络:RKResponse.m:195 NSHTTPURLRResponse状态代码:2002012-05-2419:59:09.737 GiantsGamer[57045:207]D restkit.网络:RKResponse.m:196头:{“缓存控制”=“无存储,必须重新验证”;
连接=“保持活动”;
“内容长度”=2745;
“内容类型”=“文本/html;字符集=utf-8”;
日期=“2012年5月25日星期五02:59:09 GMT”;
Expires=“”;
“保持活动”=“超时=5,最大=100”;
Pragma=“”;
Server=“Apache/2.2.21(Unix)mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8”;
“设置Cookie”=“PIWIK_sessiond=a065dba239819175689b1e2a23021d01;路径=/;HttpOnly”;
“X-Frame-Options”=sameorigin;
“X-Powered-By”=“PHP/5.3.8”;
}
2012-05-2419:59:09.737 GiantsGamer[57045:207]T restkit.网络:RKResponse.m:203读取响应主体:
…
...

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

您的代码不起作用,因为您将模块和操作作为POST值而不是GET发送。您可能会收到相同的html代码,就像您在没有参数的情况下请求index.php一样。如果您愿意,您可以尝试您的代码并捕获整个HTML响应,并将其与web上的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:

RK的主要缺点是缺乏良好的文档,以下是一些您可以了解的基础知识:

您可以看到通过RestKit使用RKObjectMapping执行所需操作的另一种更高级的方法:

最后,您还可以检查这段代码,它描述了如何


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

您是否尝试过将所有参数作为path变量上的简单字符串发送?你对此有什么反应?我试过了,效果很好。。。不过,我必须硬编码身份验证值。谢谢,@clopez,这对我来说非常有效。我仍然有点困惑,为什么我原来的代码不起作用。是的,我也认为RKObjectMapping应该适用于高级查询。我会更新我的回答来回答这个问题,如果你愿意,请记住我的回答:)我可以问一个后续问题吗?如何在NSMutableDictionary中包含多格式图像?
    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>
- (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!
            }
        };

    }];
}