分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

OC -网络请求 - NSURLConnection - GET

发布时间:2023-09-06 02:03责任编辑:苏小强关键词:GET
 ?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.1108 ????//[NSURLConnection connectionWithRequest:request delegate:self];109 ????110 ????//3.2111 ????//[[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 NSURLConnectionDataDelegate125 //1.当接收到服务器响应的时候调用126 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response127 {128 ????NSLog(@"%s",__func__);129 }130 131 //2.接收到服务器返回数据的时候调用,调用多次132 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data133 {134 ?????NSLog(@"%s",__func__);135 ????136 ????//拼接数据137 ????[self.resultData appendData:data];138 }139 //3.当请求失败的时候调用140 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error141 {142 ?????NSLog(@"%s",__func__);143 }144 145 //4.请求结束的时候调用146 -(void)connectionDidFinishLoading:(NSURLConnection *)connection147 {148 ?????NSLog(@"%s",__func__);149 ????150 ????NSLog(@"%@",[[NSString alloc]initWithData:self.resultData encoding:NSUTF8StringEncoding]);151 }152 @end

OC -网络请求 - NSURLConnection - GET

原文地址:https://www.cnblogs.com/qingzZ/p/9291109.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved