-1

在我当前的项目中,我需要调用一个基于 REST 的 Web 服务,我的同事创建了一个简单的 Web 服务(nodeJS 和 express 框架),它以 json 格式返回数据。我成功地使用NSUrlConnection. 现在我的同事将身份验证应用于服务,首先我尝试使用NSUrlConnection然后AFNetworking然后RestKit但没有成功。

RestKit 的搜索教程很旧(restkit 版本 0.10)或没有可用的身份验证教程,我想要做的就是调用一个简单的基于 REST 的带有凭据的 Web 服务。需要帮助挣扎了两天。

4

4 回答 4

1

显然,Apple 的本机 HTTP 方法已经回答了这个问题。我不确定您在使用 AFNetworking 时遇到了什么问题,但在这里的 AFHTTPClient 中,您显然可以调用其中任何一个

- (void)setAuthorizationHeaderWithUsername:(NSString *)username
                                  password:(NSString *)password;

- (void)setAuthorizationHeaderWithToken:(NSString *)token;

取决于您的网络服务。由于 RestKit 是建立在 AFNetworking 之上的,它RKObjectManager有一个httpClient属性是AFHTTPClientshow 的一个实例,你也可以调用上面的方法(从这里)。

然后你可以打电话

- (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

这里

于 2013-03-22T14:16:39.193 回答
1

要使用 RestKit 0.20 处理 GET/POST 请求/响应,您可以按照以下顺序:-

首先使用基本服务器 URL 配置 RKObjectManager :-

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseUrl];
[manager.HTTPClient setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
manager.requestSerializationMIMEType = @"application/json";

由于 RestKit 总是处理请求和响应的对象,因此您必须创建一个具有您期望响应的所有参数的对象。

@interface AuthenticationRequest : NSObject
@property (nonatomic, copy) NSNumber *userName;
@property (nonatomic, copy) NSString *password;
@end

@interface AuthenticationResponse : NSObject
@property (nonatomic, copy) NSNumber *token;
@property (nonatomic, copy) NSString *expiryDate;
@property (nonatomic, copy) NSString *userId;
@end

然后使用服务器 JSON 响应中的键为本地对象中的实例变量配置请求和响应映射。

注意:仅在 POST 或 PUT 请求的情况下配置请求映射。

RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[AuthenticationRequest class]];
[requestMapping addAttributeMappingsFromDictionary:@{
                @"userName": @"userName",
                @"password" : @"password",
            }];


RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AuthenticationResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{
                @"TOKEN": @"token",
                @"expiryDate" : @"expiryDate",
                @"USERID": @"userId"
            }];

然后创建一个响应描述符,该描述符将根据您传递的 pathPattern 值执行服务器 JSON 对象与您的本地对象的映射。

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping  objectClass:[AuthenticationResponse class] rootKeyPath:nil]
[manager addRequestDescriptor:requestDescriptor];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:<rest of the path excluding the baseURL> keyPath:nil statusCodes:nil];
    [manager addResponseDescriptor:responseDescriptor];

现在通过以下方式在服务器上执行 GET 请求:-

[manager getObjectsAtPath:(NSString *)<rest of the path excluding the baseURL>              parameters:(NSDictionary *)parameters
                 success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                 failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure];

或通过以下方式在服务器上执行 POST 请求:-

[manager postObject:AuthenticationRequest
              path:<rest of the path excluding the baseURL>
           success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
           failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure];

成功和失败块将保留您对响应的处理。

如需更多帮助,您可以参考来自 RestKit 的以下链接:-

https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md
于 2013-03-23T12:13:09.040 回答
0

在 NSURLConnectionDelegate

  • (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

方法将在那里,它将帮助您进行身份验证。

于 2013-03-22T11:18:45.167 回答
0

credential = [NSURLCredential credentialWithUser:userName password:passWord persistence:NSURLCredentialPersistenceNone];

[[挑战发件人] useCredential:credential forAuthenticationChallenge:challenge];

于 2013-03-22T11:28:54.470 回答