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

////GET:将请求所需要传的参数,直接封装到URL中,缺点:1、不安全  2url长度有限制,如果参数较多,会封装失败,导致无法请求
//POST:包含基础的url以及参数体,也就是说,参数跟url是分开的,比较安全且不用顾虑url的长度
//同步:当执行某个操作时,只有当其完全执行结束,才回去执行下面的操作,缺点:如果遇到耗时操作,会比较卡
//异步:多个任务同时执行

#import "ViewController.h"

#define BaseUrl @"http://www.haninfo.cc:2060/Login/LoginData.asmx/Login"
@interface ViewController ()<NSURLConnectionDelegate,NSURLSessionDataDelegate>{
     NSMutableData *_muData;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //8---调用同步GET
    [self synchronizeGET];
    //5*&*******调用异步GET
    [self asynchronizeGET];
}
//同步GET
-(void)synchronizeGET{
    //1---获取网络接口----//初始化一个包含参数的完整的字符串(接口地址)
    NSString * urlDtr = [NSString stringWithFormat:@"%@?sLogin=%@&sVerifyCode=%@&sPadId=%@",BaseUrl,@"yyj",@"",@""];
    //2---将接口字符串转换为url
    NSURL * url = [NSURL URLWithString:urlDtr];
    //3---发送URL请求-----//创建请求,第一个参数代表请求的地址,第二个参数代表缓存机制的选择,第三个参数代表超时时间
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];
    //4---用于获取请求--//用于获取服务器返回的头信息
    NSURLResponse * response = nil;
    //5----用于获取错误信息
    NSError * err = nil;
    //6---将URL转换为数据---//发送一个同步请求,第一个参数是请求的request,第二个参数:用于接收头信息的对象的地址,第三个参数:用于接收错误信息的对象的地址
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    //7----解析数据
    id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",obj);
   
}




-(void)asynchronizeGET{
    //1********获取网络接口
    NSString * urlDtr = [NSString stringWithFormat:@"%@?sLogin=%@&sVerifyCode=%@&sPadId=%@",BaseUrl,@"yyj",@"",@""];
    //2********将接口字符串转换为url
    NSURL * url = [NSURL URLWithString:urlDtr];
    //3********发送URL请求
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];      
    //4********发送异步请求
    //方式一:block
    //    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse    *response, NSData *data, NSError *connectionError) {
    //        id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"block:%@",obj);
//    }];
  
    //方式二,比较常用--(用一下三个代理方法)
    [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{
    //请求结束,可以在此方法进行解析
    id obj = [NSJSONSerialization JSONObjectWithData:_muData options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"代理:%@",obj);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/yibadao/p/5022768.html