BasicAuthentication和RestKit

时间:2013-12-20 20:13:00

标签: ios restkit basic-authentication

我对RestKit和BasicAuth有疑问。

我尝试使用Basic Auth的服务器API,并带有以下示例代码:

RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"test" password:@"test"];
    [objectManager getObjectsAtPath:@"/server/testapi"
                         parameters:nil
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                NSArray* statuses = [mappingResult array];
                                NSLog(@"Loaded statuses: %@", statuses);

                            }
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                message:[error localizedDescription]
                                                                               delegate:nil
                                                                      cancelButtonTitle:@"OK"
                                                                      otherButtonTitles:nil];
                                [alert show];
                                NSLog(@"Hit error: %@", error);
                            }];

但是我得到了这个错误:

E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200), got 401" UserInfo=0xb44ad30 {NSLocalizedRecoverySuggestion={"detail": "Authentication credentials were not provided."}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xda5c650> { URL: http://myhost/match/calendar }, NSErrorFailingURLKey=http://myhost/match/calendar, NSLocalizedDescription=Expected status code in (200), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda2c4f0> { URL: http://myhost/match/calendar/ } { status code: 401, headers {
    Allow = "GET, POST, HEAD, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Sat, 21 Dec 2013 13:27:34 GMT";
    Server = "WSGIServer/0.1 Python/2.7.3";
    Vary = Accept;
    "Www-Authenticate" = "Basic realm=\"api\"";
} }}

在我的AFHTTPClient中,我看到了:

{
    Accept = "application/json";
    "Accept-Encoding" = "gzip, deflate";
    "Accept-Language" = "it;q=1, en;q=0.9, fr;q=0.8, de;q=0.7, zh-Hans;q=0.6, zh-Hant;q=0.5";
    Authorization = "Basic dGVzdDpmYW50YQ==";
    "User-Agent" = "iFanta/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)";
}

但在我的服务器上我没有看到对应的HTTP标头。为什么默认AFHTTPClient标头与HTTP标头请求不对应?

如果我使用浏览器测试它运行良好;

如果我在服务器上禁用基本身份验证,则效果很好;

0 个答案:

没有答案