objective-c - AFNetworking 简单 HTTP 请求

标签 objective-c xcode afnetworking

我正在使用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/

相关文章:

iphone - 屏幕旋转锁定时如何获取设备方向?

objective-c - UIControlStateHighlighted 的奇怪问题

ios - 更改 NSError 的本地化描述语言

ios - 尝试复制文件时出错

objective-c - NSUserDefaults Plist 在 Swift 中创建失败(但 Objective-C 则不然)

iphone - iPhone 应用程序中的异常 : Modal transition is already in progress

ios - UICollectionReusableView 内部的标签始终为零

ios - 使用 VPN 连接在 iOS 模拟器中运行应用程序

objective-c - 使用 Core Plot Xcode 4.3.2 iOS 链接器命令失败

iphone - 从 iPhone 应用程序使用 AFNetworking 上传视频