您的位置:首页 > 理论基础 > 计算机网络

只适用于POST的网络请求

2016-01-19 20:23 357 查看
(void) networkManager : (NSString *)urlString

parameter: (NSDictionary *)paraDic

success:(void(^)(id obj)) success

fail:(void(^)(NSError *error))fail

{

// NSURLSession 配置

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

//创建 sessionManager

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

//创建请求

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@”POST” URLString:urlString parameters:paraDic constructingBodyWithBlock:nil error:nil];

//创建请求任务

NSURLSessionDataTask dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {

//如果有 error , 请求失败

//反之, 请求成功

if (error) {

fail(error);

} else {

success(responseObject);

}

}];

//开启任务

[dataTask resume];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: