异步post请求之代理方法

  1 #import "ViewController.h"
  2 #import "Header.h"
  3
  4 @interface ViewController ()<NSURLSessionDataDelegate>
  5
  6 /**
  7  *  用于保存相关的数据
  8  */
  9 @property (nonatomic, strong) NSMutableData *resultData;
 10
 11 @end
 12
 13 @implementation ViewController
 14
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     // Do any additional setup after loading the view, typically from a nib.
 18 }
 19
 20 // 对数据进行加载:使用NSURLSessionDataTask和NSURLSessionTask两者没有本质区别
 21 // 要处理下载任务的使用使用此任务NSURLSessionDownloadTask
 22 // 要处理上传任务使用:NSURLSessionUploadTask
 23
 24 #pragma mark - post请求(异步)
 25 - (IBAction)postRequest:(UIButton *)sender {
 26
 27     // 1.创建url
 28     NSURL *url = [NSURL URLWithString:GET_URL];
 29
 30     // 2.创建请求
 31     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 32
 33
 34     // 2.5.设置body
 35     // 创建一个连接字符串(这个内容在以后的开发中接口文档都有标注)
 36     NSString *dataStr = POST_BODY;
 37
 38     // 对连接字符串进行编码【这一步千万不能忘记】
 39     NSData *postData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
 40
 41     // 设置请求格式为post请求【在这里POST必须大写】
 42     [request setHTTPMethod:@"POST"];
 43
 44     // 设置请求体(body)
 45     [request setHTTPBody:postData];
 46
 47     // 3.创建session对象并设置代理
 48     // 参数一:模式的设置
 49     /*
 50      defaultSessionConfiguration 默认会话模式
 51      ephemeralSessionConfiguration 瞬时会话模式
 52      backgroundSessionConfigurationWithIdentifier 后台会话模式
 53      */
 54     // 参数二:代理
 55     // 参数三:线程队列
 56     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
 57
 58
 59     // 4.创建task对象
 60     NSURLSessionDataTask *task = [session dataTaskWithURL:url];
 61
 62
 63     // 5.启动任务
 64     [task resume];
 65
 66     // 对数据进行加载:使用NSURLSessionDataTask和NSURLSessionTask两者没有本质区别
 67     // 要处理下载任务的使用使用此任务NSURLSessionDownloadTask
 68     // 要处理上传任务使用:NSURLSessionUploadTask
 69
 70 }
 71
 72 #pragma mark - 实现协议方法
 73 // 服务器开始响应
 74 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
 75
 76     // 允许服务器响应【在这个地方只有允许服务器响应了才会接收到数据】
 77     completionHandler(NSURLSessionResponseAllow);
 78
 79     // 初始化data,稍后进行片段的拼接存储
 80     self.resultData = [NSMutableData data];
 81 }
 82
 83 // 接收数据拼接
 84 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
 85
 86     // 反复执行,然后拼接相关的片段
 87     [self.resultData appendData:data];
 88 }
 89
 90 // 数据接收完成,网络请求结束
 91 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
 92
 93     // 解析
 94     if (error == nil) {
 95         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.resultData options:NSJSONReadingAllowFragments error:nil];
 96         NSLog(@"%@", dic);
 97     }
 98 }
 99
100 @end
时间: 2024-07-30 07:04:22

异步post请求之代理方法的相关文章

异步get请求之代理方法

1 #import "ViewController.h" 2 #import "Header.h" 3 4 @interface ViewController ()<NSURLSessionDataDelegate> 5 6 /** 7 * 用于保存相关的数据 8 */ 9 @property (nonatomic, strong) NSMutableData *resultData; 10 11 @end 12 13 @implementation V

异步post请求之Block方法

1 #import "ViewController.h" 2 #import "Header.h" 3 4 @interface ViewController ()<NSURLSessionDataDelegate> 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional s

ios网络学习------2 用非代理方法实现异步post请求

#pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,因为一般来说属性都是和界面关联的,我们可以通过参数的方式来使用属性 #pragma mark post登录方法 -(void)loginWithPostWithName:(NSString *)userName pwd:(NSString *)pwd { //1确定地址NSURL NSString *urlString = [NSString stringWithFormat:@"www.baidu.com"];

弃用的异步get和post方法之代理方法

1 #import "ViewController.h" 2 #import "Header.h" 3 4 @interface ViewController () <NSURLConnectionDataDelegate> 5 6 /** 7 * 用来存储数据 8 */ 9 @property (nonatomic, strong) NSMutableData *resultData; 10 11 @property (nonatomic, stron

iOS.访问 Web Service.异步GET请求方法

#import <UIKit/UIKit.h> #import "T20140628024750NSNumber+Message.h" #import "T20140628024750NSString+URLEncoding.h" @interface T20140628024750ViewController : UITableViewController<NSURLConnectionDelegate> @property (nonato

iOS.访问 Web Service.异步POST请求方法

#import <UIKit/UIKit.h> #import "T20140628024917NSNumber+Message.h" #import "T20140628024917NSString+URLEncoding.h" @interface T20140628024917ViewController : UITableViewController<NSURLConnectionDelegate> @property (nonato

iOS_21团购_通过block对请求工具类的代理方法进行二次封装

最终效果图: [点评]提供的工具类DPAPI 在请求完毕后,使用的是代理机制,当一次请求成功或者失败时,会调用代理的相应方法 为了将点评提供的工具类DPAPI进行二次封装, 再次定义了一个block: typedef  void(^RequestDoneCallBackBlock)(id deals,NSError *err); 该block有两个参数, 分别是成功时,服务器返回的deals字典数组 另一个参数是:请求失败时,服务器返回的失败信息 两个参数分别对应代理的两个方法(即成功.失败时分

PHP异步请求之fsockopen()方法详解

正常情况下,PHP执行的都是同步请求,代码自上而下依次执行,但有些场景如发送邮件.执行耗时任务等操作时就不适用于同步请求,只能使用异步处理请求. 场景要求: 客户端调用服务器a.php接口,需要执行一个长达10s-20s不等的耗资源操作,假如客户端响应请求时间为5秒(请求响应超时时间),5s以上无回复即断开连接. 解决设想: 客户端调用a.php之后,a.php执行异步多线程操作调用b.php,a.php调用成功后即刻反馈给客户端回执,b.php自动执行耗资源操作. 方案: 利用fsockope

C# .Net FrameWork3.5中异步HTTP请求时,由于安全协议的问题System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)方法抛出“基础连接已经关闭: 发送时发生错误”的解决办法

现象描述: C# .Net FrameWork3.5中异步HTTP请求时,由于安全协议的问题System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)方法抛出“基础连接已经关闭: 发送时发生错误”. 原因分析: 大多数情况下是由于客户端所设置的HTTP访问请求的安全协议不符合服务器端的安全协议要求.比如,NASA提供瓦片服务的http://worldwind25.arc.nasa.gov/wms?service=WMS&v