• 网络请求数据(同步POST,异步POST)


    //同步POST
    -(void)synPost{
        //获取URL接口,不含参数
        NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
        //转码---拼接方法,为避免参数是汉字时,打印结果不显示汉字
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
       
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
       
        //post方式的参数封装
        NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
        //将参数列表转换成data
        NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
        //设置参数体
        [request setHTTPBody:data];
        //设置请求方式
        [request setHTTPMethod:@"POST"];
       
        //发送请求
        NSData *responData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        //解析
        id obj = [NSJSONSerialization JSONObjectWithData:responData options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",obj);
       
    }
    =====================================================
    //异步POST
     
    -(void)asynPOST{
        NSString *str = @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login";
       
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//把汉字转换成字符串
        NSURL *url = [NSURL URLWithString:str];
       
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
       
        //post方式的参数封装
        NSString *dataStr = [NSString stringWithFormat:@"sLogin=%@&sVerifyCode=%@&sPadId=%@",@"yyj",@"",@""];
        //将参数列表转换成data
        NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
        //设置参数体
        [request setHTTPBody:data];
        //设置请求方式
        [request setHTTPMethod:@"POST"];
        
    //方法一:系统封装好的(不在需要代理方法)
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@",dic);
        }];
     
    //方法二:需要代理方法
        [NSURLConnection connectionWithRequest:request delegate:self];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        _muData = [NSMutableData data];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
       
        [_muData appendData:data];
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //解析
    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        //连接服务器失败
        NSLog(@"错误信息:%@",error);
    }
  • 相关阅读:
    ThinkPHP3.2 整合支付宝RSA加密方式
    代码风格规范
    Mac下安装composer
    MAC 下安装RabbitMQ
    Redis配置
    git 分支
    PHP常用数组操作方法汇总
    php 不用第三个变量 交换两个变量的值汇总
    PHP配置错误信息回报的等级
    Apache同一个IP上配置多域名
  • 原文地址:https://www.cnblogs.com/yibadao/p/5022775.html
Copyright © 2020-2023  润新知