1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<NSURLConnectionDataDelegate>
  4 /** 注释 */
  5 @property (nonatomic, strong) NSMutableData *resultData;
  6 @end
  7 
  8 @implementation ViewController
  9 
 10 #pragma mark ----------------------
 11 #pragma mark lazy loading
 12 -(NSMutableData *)resultData
 13 {
 14     if (_resultData == nil) {
 15         _resultData = [NSMutableData data];
 16     }
 17     return _resultData;
 18 }
 19 #pragma mark ----------------------
 20 #pragma mark Events
 21 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 22 {
 23     [self delegate];
 24 }
 25 
 26 /*
 27  请求:请求头(NSURLRequest默认包含)+请求体(GET没有)
 28  响应:响应头(真实类型--->NSHTTPURLResponse)+响应体(要解析的数据)
 29  */
 30 #pragma mark ----------------------
 31 #pragma mark Methods
 32 -(void)sync
 33 {
 34     /*
 35      GET:http://120.25.226.186:32812/login?username=123&pwd=456&type=JSON
 36      协议+主机地址+接口名称+?+参数1&参数2&参数3
 37      post:http://120.25.226.186:32812/login
 38      协议+主机地址+接口名称
 39      */
 40     //GET,没有请求体
 41     //1.确定请求路径
 42     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
 43     
 44     //2.创建请求对象
 45     //请求头不需要设置(默认的请求头)
 46     //请求方法--->默认为GET
 47     NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
 48     
 49     //3.发送请求
 50     //真实类型:NSHTTPURLResponse
 51     NSHTTPURLResponse *response = nil;
 52     /*
 53      第一个参数:请求对象
 54      第二个参数:响应头信息
 55      第三个参数:错误信息
 56      返回值:响应体
 57      */
 58     //该方法是阻塞的,即如果该方法没有执行完则后面的代码将得不到执行
 59     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
 60     
 61     //4.解析 data--->字符串
 62     //NSUTF8StringEncoding
 63     NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
 64     
 65     NSLog(@"%zd",response.statusCode);
 66 }
 67 
 68 -(void)async
 69 {
 70     //1.确定请求路径
 71     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
 72     
 73     //2.创建请求对象
 74     //请求头不需要设置(默认的请求头)
 75     NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
 76     
 77     //3.发送异步请求
 78     /*
 79      第一个参数:请求对象
 80      第二个参数:队列 决定代码块completionHandler的调用线程
 81      第三个参数:completionHandler 当请求完成(成功|失败)的时候回调
 82         response:响应头
 83         data:响应体
 84         connectionError:错误信息
 85      */
 86     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
 87         
 88        
 89         //4.解析数据
 90         NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
 91         
 92         NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
 93         NSLog(@"%zd",res.statusCode);
 94         NSLog(@"%@",[NSThread currentThread]);
 95     }];
 96 }
 97 
 98 -(void)delegate
 99 {
100     //1.确定请求路径
101     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"];
102     
103     //2.创建请求对象
104     NSURLRequest *request = [NSURLRequest requestWithURL:url];
105     
106     //3.设置代理,发送请求
107     //3.1
108     //[NSURLConnection connectionWithRequest:request delegate:self];
109     
110     //3.2
111     //[[NSURLConnection alloc]initWithRequest:request delegate:self];
112     
113     //3.3 设置代理,时候发送请求需要检查startImmediately的值
114     //(startImmediately == YES 会发送 | startImmediately == NO 则需要调用start方法)
115     NSURLConnection * connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
116     
117     //调用开始方法
118     [connect start];
119     
120 //    [connect cancel];//取消
121 }
122 
123 #pragma mark ----------------------
124 #pragma mark NSURLConnectionDataDelegate
125 //1.当接收到服务器响应的时候调用
126 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
127 {
128     NSLog(@"%s",__func__);
129 }
130 
131 //2.接收到服务器返回数据的时候调用,调用多次
132 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
133 {
134      NSLog(@"%s",__func__);
135     
136     //拼接数据
137     [self.resultData appendData:data];
138 }
139 //3.当请求失败的时候调用
140 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
141 {
142      NSLog(@"%s",__func__);
143 }
144 
145 //4.请求结束的时候调用
146 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
147 {
148      NSLog(@"%s",__func__);
149     
150     NSLog(@"%@",[[NSString alloc]initWithData:self.resultData encoding:NSUTF8StringEncoding]);
151 }
152 @end

 

posted on 2018-07-10 19:43  懂事长qingzZ  阅读(276)  评论(0编辑  收藏  举报