知识点
1.HTTP协议的简介
2.URLConnection同步请求
 1 //1转化成URL
 2     NSURL * url = [NSURL URLWithString:QQURLSTING];
 3     //2生成请求对象
 4     NSURLRequest * request = [NSURLRequest requestWithURL:url];
 5     NSURLResponse * response = nil;
 6     NSError * error = nil;
 7     //3建立链接 发起同步请求
 8     // 参数1 请求对象
 9     //参数2 响应对象的地址
10     //参数3 请求失败的NSError的对象地址
11     NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
12     
13     //4.json数据解析
14     NSError * jsonError = nil;
15     
16     //参数1 二进制数据
17     //参数2 枚举 一般用NSJSONReadingMutableContainers  可变的容器
18     //参数3 错误信息  如果有错误  则会创建NSError对象
19     NSDictionary * dic  = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
20     
21     
22     NSLog( @"dic %@",dic);
23     //结论: 使用同步请求,如果耗时 造成UI界面的假死
24     // 同步  相对主线程来说
3.URLConnection异步请求
 1  //生成URL对象
 2     NSURL * url = [NSURL URLWithString:URLSTRING];
 3     
 4     //创建请求对象
 5     NSURLRequest * request = [NSURLRequest requestWithURL:url];
 6     
 7     //根据请求对象 创建连接对象
 8     //参数1  请求对象
 9     //参数2  代理(找到这个对象  发生消息)
10     _connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
11     
12     //向服务器发起请求
13     [_connection start];
 1 //代理方法
 2 //收到响应  调用这个方法 被调用一次(相对于一次请求)
 3 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
 4     NSLog(@"response %@",response);
 5     
 6     //如果存在  清空Data
 7     if (_downloadData) {
 8         //清空
 9         _downloadData.length = 0;
10     }else{
11         //不存在 则初始化
12         _downloadData = [[NSMutableData alloc]init];
13     }
14 }
15 //接收数据   服务器发送数据是一段段的  该协议方法 会被调用多次
16 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
17     NSLog(@"%@",data);
18     //将每次获取的二进制数据拼接起来
19     [_downloadData appendData:data];
20 }
21 
22 //传输完成 调用协议方法
23 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
24     NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:_downloadData options:NSJSONReadingMutableContainers error:nil];
25     NSLog(@"dic %@",dic);
26 }
27 //请求失败的时候 会调用这个协议方法
28 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
29     NSLog(@"error %@",error);
30 }
4.URLConnection异步请求Block版本
 1 //1生成NSURL
 2     NSURL * url = [NSURL URLWithString:QQURLSTING];
 3     NSURLRequest * request = [NSURLRequest requestWithURL:url];
 4     //建立简介  发起异步请求
 5     //参数1 请求对象
 6     //参数2 队列 block 再哪个队列执行 [NSOperationQueue mainQueue] 主队列
 7     //参数3 block回调
 8     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
 9         if (connectionError == nil) {
10             //请求成功
11             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
12             NSLog(@"dic %@",dic);
13         }else{
14             //请求失败  网络原因,网址错误 请求资源不存在 请求没有权限
15             NSLog(@"error %@",connectionError);
16         }
17     }];
5.URLConnectionPOST请求
 1     //常用两种请求方式   GET和POST请求
 2     
 3     
 4     //如何拼接参数: ?参数名称=参数(具体值)&参数名称=参数(具体值)
 5     //如果有多个参数 使用&分割
 6     //1生成URL
 7     NSURL * url = [NSURL URLWithString:SNSURLSTRING];
 8     //创建url一个请求对象  可变的
 9     NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url];
10     
11     //设置请求方式 默认GET请求,一定大写
12 //    request.HTTPMethod = @"GET";
13     request.HTTPMethod = @"POST";
14     //拼接参数
15     NSString *para = @"page=1&number=2";
16     //设置请求体 post把参数放在请求体中
17     request.HTTPBody = [para dataUsingEncoding:NSUTF8StringEncoding];
18     
19     
20     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
21         if (connectionError == nil) {
22             //请求成功
23             //存入字典 解析JSON数据
24             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
25             NSLog(@"dic %@",dic);
26         }else{
27             //请求失败 打印失败地址
28             NSLog(@"error %@",connectionError);
29         }
30     }];
6.URLConnection的封装
=====================================
1.HTTP协议
1).
发送一次请求
Client -------------------------> Server
<-------------------------
对应收到一次响应
响应结束连接断开
2). 一个HTTP请求包含:
URL // 请求地址
Method // GET、POST等
Header // 请求头、请求的信息
Body // 请求体,POST用到
一个HTTP响应包含:
Header // 响应头、响应的信息
Body // 响应体,我们想得到的数据
=====================================
2.NSURL,用OC中用来表示URL的类
创建URL:
[NSURL URLWithString:urlStr];
=====================================
3.NSURLRequest,用来表示一个请求
创建Request:
[NSURLRequest requestWithURL:url];
创建带超时的Request
[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:0.01f];
创建可变的Request,可修改Request的属性,如Method等,POST用到
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
设置请求体
NSString *paramStr = @"username=qf1508&password=123456";
request.HTTPBody = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
=====================================
4.NSURLConnection,表示一个请求响应的连接
发送同步请求
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
发送异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {}];
发送异步请求,delegate版本
[NSURLConnection connectionWithRequest:request delegate:self];
=====================================
4.NSURLConnection的Delegate回调函数
接收到服务器的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
接收到服务器发送过来的数据,数据是分段发给我们的,所以这个方法会被调用多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
服务器所有的数据都发送完成之后,会调用这个方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
请求失败(url不合法、超时、网络中断等等)时会调用这个方法
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
原文:http://www.cnblogs.com/gwkiOS/p/5027938.html