IOS AFN passes parameters to the server through the body

IOS AFN passes parameters to the server through the body

2017-07-10 20:19Walking  in the gravel 

  1. This piece of code is suitable for passing parameters in json format when requesting an interface, rather than a common dictionary. At the same time, set the Content-Type of the request header: application/json;charset=UTF-8
  2. Import the AFN file or pod first.

The default Content-Type of AFNetwoking is application/x-www-form-urlencodem. If the server requires the Content-Type to be applicaiton/json, in order to correspond with the server, you must modify the Content-Type of AFNetworking

=========== I just saw more trouble-free code in other bloggers, the following code is not recommended. You can skip using the second code block directly ===

NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};

//设置参数 根据你们服务器的格式设置。我们的后台需要传的是json格式的

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters  
 options:NSJSONWritingPrettyPrinte  error:nil];

 //afn请求
 AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: 
 [NSURLSessionConfiguration defaultSessionConfiguration]];

      NSString * requestUrl  = @"你的请求地址";
    //如果你不需要在请求体里传参 那就参数放入parameters里面
      NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] 
       requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];

    // NSLog(@"requestURL:%@",requestUrl);
     request.timeoutInterval= 10;

    //这句话很重要,设置"Content-Type"类型 json类型跟后台大哥的一致
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    // 设置参数放入到body请求体里。后台大哥让参数放在请求体里,因为没写这句代码,我TM调试浪费了半天
    [request setHTTPBody:jsonData];

    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                 @"text/html","text/json", @"text/javascript",@"text/plain",  nil];

        manager.responseSerializer = responseSerializer;

    [[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil         
      completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){

           if(responseObject!=nil){

               NSString *result = [[NSString alloc] initWithData:responseObject  
           encoding:NSUTF8StringEncoding];
           NSLog(@"%@",result);

           }

       }]resume];

    The second solution: recommended

  
   NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};



    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];
    //post 发送json格式数据的时候加上这两句。
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    session.requestSerializer.timeoutInterval = 15;
//     NSString * requestUrl  = @"http://47.244.143.48:8080/auth/getVerificationCode";
    NSString * requestUrl  = @"http://192.168.1.10:8080/auth/getVerificationCode";
    [session POST:requestUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSLog(@"post成功了%@",responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"post失败了%@",error);
            }
    }];

 

Guess you like

Origin blog.csdn.net/ximiaoweilai/article/details/102942632