NSURLConnection发送GET请求

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