AFNetworking application/x-www-form-urlencoded 數據請求方式


 

// 請求數據類實例化

AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

 

// 可變request實例化

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:str]];

// 設置 請求方法我POST 

    request.HTTPMethod = @"POST";

 

    // 設置請求頭 的 Content-Type格式

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSString *postStr = [NSString stringWithFormat:@"content=%@",mdic];

    [request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];

    

   // 請求數據

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

        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

        NSInteger responseStatusCode = [httpResponse statusCode];

//        NSLog(@"---------%@ %ld %@", httpResponse, (long)responseStatusCode ,responseObject);

        if (responseStatusCode == 200) {

            // 成功后的處理

//            NSLog(@"%@", responseObject);

//            NSLog(@"返回數據為!!!%@" , responseObject);

            successResponse(responseObject);

        }else {

            // 失敗后的處理

            //                                                                  NSLog(@"%@", error);

            if(error.code==-1009){

                NSDictionary *codeDic=@{@"errCode":@"-1009",@"msg":@"網絡未連接!"};

                successResponse(codeDic);

            }else{

                NSDictionary *codeDic=@{@"errCode":APPERROR,@"msg":@"未知錯誤!"};

                successResponse(codeDic);

            }

        }

    }];

    [dataTask resume];

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM