NSURLSessionDataTask发送Get/Post请求
2016-04-17 13:22
309 查看
<span style="font-size:14px;">// // ViewController.m // NSURLSession // // Created by hq on 16/4/17. // Copyright © 2016年 hanqing. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } -(void) post{ NSURLSession *session=[NSURLSession sharedSession]; NSString *urlString=@"http://xxxx/login"; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; request.HTTPMethod=@"POST"; request.HTTPBody=[@"username=aa&pwd=aa" dataUsingEncoding:NSUTF8StringEncoding]; NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }]; [task resume]; } -(void) dataTaskWithURL{ //获取session对象 NSURLSession *session=[NSURLSession sharedSession]; NSString *urlString=@"http://120.25.226.186:32812/login?username=aa&pwd=aa"; NSURLSessionDataTask *task=[session dataTaskWithURL:[NSURL URLWithString: urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }]; [task resume]; } @end </span>
相关文章推荐
- Spring初探----Bean的装配
- hdu 1712(分组背包)
- 页面并发性处理解决方案
- Expression: _pFirstBlock==pHead
- 【杭电-oj】-1161-字母全部化为小写
- 谷歌 javascript v8 引擎 5.2版 32位 Debug DLL 分享及使用方法
- Sublime Text 全程指南
- think php sql () or ()
- HDU-1002
- 网页监控之自己设计监控界面
- JAVA设计模式概述
- LeetCode House Robber I II III
- MySQL索引结构--由 B-/B+树看
- 个人进度条-第七周
- 输入数据数量未知的处理
- javascript 变量命名规则
- web新手上路
- 你努力了吗?全力以赴了吗?
- CALLBACK函数
- Metal 着色语言编程指南 六