我正在使用AFNetworking在我的项目中,我只需要检查 URL 是否成功(状态 2xx)。
我试过了
NSURLRequest *request = [NSURLRequest requestWithURL:url2check];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (response.status == 200) {
NSLog(@"Ok %@", JSON);
}
}
failure:nil
];
[operation start];
但是由于我并不是特别等待 JSON,所以我希望使用类似的东西
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation
HTTPRequestOperationWithRequest: success: failure:];
但是这个不存在(我不知道为什么),所以我用了
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:...]
还有更简单的吗?
您将如何使用 AFNetworking 进行这个简单的 URL 检查?
最佳答案
这就是我最终得到的结果
NSURLRequest *request = [NSURLRequest requestWithURL:url2check];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
, id responseObject) {
// code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// code
}
];
[operation start];
随意添加更简单的方法。
关于objective-c - AFNetworking 简单 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170487/