我无法使用 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 响应?