我想使用AFNetworking
在服务器上发送数据。我想发送NSData
而不是json参数。任何人都可以建议如何使用NSData
在服务器上发送AFNetworking
?
答案 0 :(得分:1)
您可以在AFHTTPRequestOperationManager
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
您使用该方法的方式是:
[self POST:@"http://myurl.com" parameters:@{} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// attach the NSData to `formData`
} success:^(NSURLSessionDataTask *task, id responseObject) {
// Handle Success
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// Handle Error
}];
为了附加数据,您可以在AFMultipartFormData
协议
- (void)appendPartWithFormData:(NSData *)data
name:(NSString *)name;