zoukankan      html  css  js  c++  java
  • iOS网络基础

    Demo

    #import "SDViewController.h"
    
    @interface SDViewController ()
    @property (weak, nonatomic) IBOutlet UITextField *userName;
    @property (weak, nonatomic) IBOutlet UITextField *password;
    
    @end
    
    @implementation SDViewController
    
    /**
     *  网络请求步骤
     *  1、URL 确定资源
     *  NSString *urlStrl = @"";
     *  NSURL *url = [NSURL URLWithString:urlStrl];
     *  
     *  (1)GET   URL中包含参数
     *  (2)POST  URL中包含参数
     *  
     *  2、建立请求 URLRequest
     *  (1)    GET 不需要对请求参数做处理
     *          URLRequest
     *
     *  (2)    POST 需要在请求中包装参数, 指定HTTP方法和HTTP数据体
     *       NSMutableURLRequest
     *      HTTPMethod = @"POST"
     *       HTPPBody = 包含登录信息的二进制数据
     *  3、发送请求
     *       !!!在实际开发中,所有的网络请求都是异步的
     *      NSURLConnection sendAsynchronousRequest
     *       在请求的异步方法中,对接收到的数据进行处理!
     */
    
    //登陆按钮点击事件
    - (IBAction)loginBtnClick:(id)sender
    {
    //    [self getLoginWithUserName:self.userName.text andPassword:self.password.text];
        [self postLoninWithUserName:self.userName.text andPassword:self.password.text];
    }
    
    
    /** GET请求登陆*/
    - (void)getLoginWithUserName:(NSString *)userName andPassword:(NSString *)password
    {
        //1、url -- 准备资源
        NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@",userName,password];
        NSURL *url = [NSURL URLWithString:urlStr];
        
        //2、创建网络请求
        //URLRequest请求默认是GET
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        //3、发送网络请求
        //所有的网络请求都是使用异步方法
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 1> 将返回的二进制数据,转换成字符串
            // (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData
            // (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!
            // 转换成字符串的方式,在调试中经常使用!
            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"result -- %@",result);
            
            NSString *respond = [NSString stringWithFormat:@"respond -- %@",response];
            NSLog(@"respond -- %@",respond);
            
            //JSON转换,格式是和NSDictionary的快速包装方式非常像
            //将JSON转换成字典
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
            
            NSLog(@"dict -- %@",dict);
        }];
        
    }
    
    
    /** POST请求登陆*/
    - (void)postLoninWithUserName:(NSString *)userName andPassword:(NSString *)password
    {
        //1、url  准备资源
        NSString *urlStr = @"http://localhost/login.php";
        NSURL *url = [NSURL URLWithString:urlStr];
        
        //2、request,POST方法,需要建立一个可变的请求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        request.HTTPMethod = @"POST";  //设置请求方式
        
        //2.1数据体
        NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",userName,password];
        //将字符串转换成二进制数
        request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
        
        //3、发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 1> 将返回的二进制数据,转换成字符串
            // (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData
            // (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!
            // 转换成字符串的方式,在调试中经常使用!
            NSLog(@"response - %@",response);
            
            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"result -- %@",result);
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
            NSLog(@"dict -- %@",dict);
            
            NSError *erro  = connectionError;
            NSLog(@"erro -- %@",erro);
            
             }];
        
    }
    
    
    
    @end
    

      

  • 相关阅读:
    Appium安装教程
    方法(method)和函数(function)有什么区别?
    FTP两种工作模式:主动模式(Active FTP)和被动模式介绍
    python socket编程介绍
    面向对象基础篇
    python fishc.homework2
    python遇到的问题汇总
    我对 python 面向对象的理解
    深入理解JVM(五)JVM优化策略
    深入理解JVM(四)JVM性能监控与故障处理工具
  • 原文地址:https://www.cnblogs.com/ndyBlog/p/3971291.html
Copyright © 2011-2022 走看看