我正在尝试使用AFHTTPClient进行rest 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];发布于 2013-03-19 10:29:21
关于AFHTTPClient的一件事是在初始化方法中设置基本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];https://stackoverflow.com/questions/15489850
复制相似问题