创建网络get请求

两种常用方式
1、异步请求

// 0.根据用户名和密码拼接URL
    // 创力url:
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
@"http://122.33.44.55:8586/login_check_byPhone?userName=%@&passWord=%@",userName,pwd]];
    //NSLog(@"url:%@",url);
    
    // 1.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 2.发送请求,创建一个子线程进行监控服务器响应的数据
   [NSURLConnection sendAsynchronousRequest:request  //发送请求
                                       queue:[[NSOperationQueue alloc] init] //创建一个子线程
                           completionHandler:^(NSURLResponse * _Nullable //回调Block  返回的头
                                               response, NSData * _Nullable data, //返回的数据体
                                               NSError * _Nullable connectionError) { //返回的错误
        
        // 3.响应数据解析
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        // HUD必须要在主线程才能进行操作
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if ([str containsString:@"success"]) {
                [SVProgressHUD showSuccessWithStatus:@"登陆成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:@"登陆失败"];
            }
        }];
    }];

2、代理方法

// 创力url:
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123"];
    //NSLog(@"url:%@",url);
    
    // 1.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 2.创建请求连接
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

#pragma mark - NSURLConnectionDataDelegate  常用的几个代理方法
/**
 *  请求失败
 *
 *  @param connection
 *  @param error
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"%s,%d-----%@",__func__,__LINE__,error);
}

/**
 *  开始接收数据,如果数据比较大,会多次调用该函数
 *
 *  @param connection
 *  @param data
 */
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%s,%d----%@",__func__,__LINE__,str);
    
}

/**
 *  接收服务器的响应
 *
 *  @param connection
 *  @param response
 */
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"%s,%d----%@",__func__,__LINE__,response);
    
}

/**
 *  数据接收完成后调用
 *
 *  @param connection
 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"%s,%d",__func__,__LINE__);
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • AFHTTPRequestOperationManager 网络传输协议UDP、TCP、Http、Socket、X...
    Carden阅读 4,590评论 0 12
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,045评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,808评论 25 709
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,219评论 6 13
  • 不要给你的放肆任何借由 前几日的来稿中,有一篇讲述青春放肆的爱情,故事中的两人在初中没毕业就结婚生子,作者说很多时...
    d8ff15d07d8d阅读 379评论 0 1