首页 > 代码库 > AFNetworking application/x-www-form-urlencoded 数据请求方式

AFNetworking application/x-www-form-urlencoded 数据请求方式

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "SimSun-ExtB"; min-height: 12.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "SimSun-ExtB" } span.s1 { font: 12.0px "SimSun-ExtB"; color: #000000 } span.s2 { } span.s3 { color: #3d1d81 } span.s4 { font: 12.0px "SimSun-ExtB" } span.s5 { color: #703daa } span.s6 { color: #d12f1b } span.s7 { color: #31595d } span.s8 { color: #bb2ca2 } span.s9 { font: 11.0px Menlo; color: #703daa } span.s10 { font: 11.0px Menlo; color: #3d1d81 } span.s11 { font: 11.0px Menlo; color: #bb2ca2 } span.s12 { font: 11.0px Menlo; color: #272ad8 } span.s13 { font: 11.0px Menlo; color: #008400 } span.s14 { font: 11.0px "PingFang SC"; color: #008400 } span.s15 { font: 11.0px "PingFang SC" } span.s16 { color: #272ad8 } span.s17 { color: #78492a } span.s18 { font: 11.0px "PingFang SC"; color: #d12f1b }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187 } span.s1 { } span.s2 { font: 12.0px "SimSun-ExtB"; color: #000000 } span.s3 { color: #31595d }</style>

 

// 请求数据类实例化

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];

 

 

 

 

AFNetworking application/x-www-form-urlencoded 数据请求方式