1

两天以来,我一直在寻找 RestKit API 的简单代码,用于在不使用 JSON 的情况下登录网站。这是我到目前为止编写的代码:

- (void)login
{
    [RKClient clientWithBaseURLString:@"http://Mywebsite.com/login.php"];
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:@"user" forKey:@"username"];
    [params setObject:@"pass" forKey:@"password"];
    [params setObject:@"login" forKey:@"type"];
    [[RKClient sharedClient] post:@"/login" params:params delegate:self];

}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    NSLog(@"HTTP status code:     %d", response.statusCode);
    NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
    }
    RKLogError(@"Hit error: %@", error);
}

这是我收到的日志:

2012-09-08 21:44:14.633 RestKitTest[889:fb03] I restkit:RKLog.m:33 RestKit initialized...
2012-09-08 21:44:15.010 RestKitTest[889:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c5a560 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-08 21:44:21.021 RestKitTest[889:fb03] I restkit.network:RKRequest.m:676 Status Code: 404
2012-09-08 21:44:21.036 RestKitTest[889:fb03] HTTP status code:     404
2012-09-08 21:44:21.090 RestKitTest[889:fb03] HTTP status message:  not found

任何想法如何解决此问题将不胜感激。

4

2 回答 2

2

I'd say it 404s because you set baseURL as http://Mywebsite.com/login.php yet you POST as post:@"/login". Do you really want to access http://Mywebsite.com/login.php/login?

The baseURL should be set to a common prefix to all your backend calls, in your case http://Mywebsite.com, the resource itself should be posted as post:@"/login.php". The resource name and baseURL are concatenated before the request is sent.

于 2012-09-10T06:27:17.903 回答
0

您需要提供有关login.php正在做什么的信息。如果脚本 url 正确解析,我不明白为什么它应该 404。

此外,如果该 PHP 脚本在成功时执行重定向,您应该使用该RKClient方法 post:usingBlock:并在该块中执行 do request.followRedirect = NO;。这样,如果您要返回一个状态码,您将获得正确的状态码。

如果也可能有助于记录更多响应:

NSLog(@"Header fields: %@", response.allHeaderFields);
NSLog(@"Body: %@", response.bodyAsString);
于 2012-09-09T22:12:18.853 回答