AFNetworking 3.0封装post请求body里面内容是空的

迁移到AFNetworking 3.0,网上资料较少。发现封装post,body里面内容是空的,服务端收到的params为空。

自己研究了一个正确的写法。供大家参考。

NSDictionary *param = [NSDictionarydictionaryWithObjectsAndKeys:@{@"agent":@"X-IOS",@"version":[[UIDevicecurrentDevice] systemVersion],

_smanager = [AFHTTPSessionManagermanager];

        _smanager.securityPolicy = securityPolicy;

        _smanager.responseSerializer = [AFHTTPResponseSerializerserializer];

        _smanager.operationQueue.maxConcurrentOperationCount = 10;

_smanager.requestSerializer = [AFJSONRequestSerializerserializer];

    [_smanager.requestSerializersetValue:@"agent"forHTTPHeaderField:@"X-IOS"];

    [_smanager.requestSerializersetValue:@"local"forHTTPHeaderField:country];

    [_smanager.requestSerializersetValue:@"version"forHTTPHeaderField:[[UIDevicecurrentDevice]systemVersion]];

    [_smanager.requestSerializersetValue:@"device"forHTTPHeaderField:[[UIDevicecurrentDevice]model]];

    [_smanagerPOST:url

         parameters:param

            success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject) {

                NSString *str = [responseObjectJSONString];

                NSDictionary *info = [responseObjectJSONObject];

            }

            failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {


            }];


这个写法,服务端收到的参数是正常的。

希望大家多多分享。


你可能感兴趣的:(AFNetworking,3.0,封装post,body内容为空)