#import "ViewController.h" @interface ViewController ()<NSURLSessionDataDelegate> @property (nonatomic, strong) NSMutableData *totalData; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.totalData = [[NSMutableData alloc]init]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // [self sendPost]; [self sendPostWithDelegate]; } -(void)sendPost { //直接发送POST请求 NSURL *url = [NSURL URLWithString:@"http://www.yahoo.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSString *username = @"zhaosi"; NSString *password = @"lsp188"; NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",username,password]; request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error == nil) { NSString *resultStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"-------%lu",resultStr.length); } else { NSLog(@"%@",error.description); } }]; //必须启动任务,否则不会走block中的回调 [dataTask resume]; } -(void)sendPostWithDelegate {//通过代理完成请求 NSURL *url = [NSURL URLWithString:@"http://www.yahoo.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSString *username = @"zhaosi"; NSString *password = @"lsp188"; NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",username,password]; request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; //自定义会话对象设置代理, NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];//设置代理方法在哪个线程执行 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; [dataTask resume]; } #pragma mark NSURLConnectionDataDelegate - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {//接收到服务器响应后,调用的方法 NSLog(@"didReceiveResponse"); //需要通过调用completionHandler告诉系统应该如何处理服务器返回的数据 completionHandler(NSURLSessionResponseAllow);//NSURLSessionResponseAllow表示接收返回的数据 } -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {//接收到服务器响应数据的时候会调用,该方法可能调用多次 [self.totalData appendData:data]; NSLog(@"%lu---",self.totalData.length); } -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {//请求完成 或者失败的时候调用 NSLog(@"didCompleteWithError"); //在这里 解析数据 NSLog(@"%@",[[NSString alloc]initWithData:self.totalData encoding:NSUTF8StringEncoding]); } @end
NSURLSession发送POST请求
原创wx60d5c898eb6a1 ©著作权
文章标签 NSURLSession 文章分类 后端开发
-
java 监听post 请求
本文简要介绍了使用Servlet API监听POST请求的完整示例,并给广大读者详细介绍了Servlet的概念及使用方法。
Web java 服务器 Servlet post -
Perl POST请求发送
使用Perl进行POST访问的经典方法。
POST Perl -
HTTP发送POST请求
HTTP发送POST请求
HttpUtil POST 请求 -
java_home 在哪看
一、资料文档二、开发语言三、支持的协议四、消息存储五、消息事务六、负载均衡七、集群方式八、管理界面九、可用性十、消息重复十一、吞吐量TPS十二、订阅形式和消息分发十三、顺序消息十四、消息确认十五、消息回溯十六、消息重试十七、并发度本文将从,Kafka、RabbitMQ、ZeroMQ、RocketMQ、ActiveMQ 17 个方面综合对比作为消息队列使用时的差异。一、资料文档Kafka:
java_home 在哪看 javahome在哪个配置文件里 kafka rabbitmq优劣对比 kafka topic数量上限 服务器