0

我正在尝试使用 AFHTTPClient 进行休息 api 调用。我正在尝试通过将注册时获得的身份验证密钥传递给 api 来发出 api 请求。当我运行该应用程序时,我收到 403 错误。我是使用 AFNetworking 的新手,我非常感谢任何帮助。

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL     URLWithString:@"http://somewebsite.com/"]];

[httpClient setDefaultHeader:@"Authorization: Client-ID" value:@"xxxxxxxx"];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:@"https://api.somewebsite.com/api/category/subcategory/fun/"
                                                  parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Print the response body in text
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
[operation start];
4

1 回答 1

2

AFHTTPClient 的事情是您在 init 方法中设置基本 url..然后每个 requestWithMethod 调用..您从基本传递一个相对 URL...例如,

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL     URLWithString:@"https://api.somewebsite.com/"]];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:@"/api/category/subcategory/fun/"
                                                  parameters:nil];
于 2013-03-19T02:29:20.777 回答