iOS 03-NSURLSession类 GET与POST请求
2017-09-07 16:24
183 查看
//使用步骤:
/*
1、创建NSURLSession的会话
2、根据会话创建Task
3、执行Task
*/
不废话,直接上代码。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event
{
[self
post];
}
-(void)post {
//设置请求地址
NSURL *url = [NSURL
URLWithString:@"https://tieba.baidu.com/f"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url];
//设置请求方式
request.HTTPMethod =
@"POST";
//设置请求体
request.HTTPBody = [@"kw=cos"
dataUsingEncoding:NSUTF8StringEncoding];
//创建Session
NSURLSession *session = [NSURLSession
sharedSession];
//根据会话创建任务
NSURLSessionDataTask *dataTask = [session
dataTaskWithRequest:request
completionHandler:^(NSData *
_Nullable data, NSURLResponse *
_Nullable response, NSError *
_Nullable error) {
// 打印当前线程
NSLog(@"---%@---",[NSThread
currentThread]);
if (error ==
nil) {
NSString *str = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"---%@---",str);
//解析JSON数据,用字典接收
NSDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
}
}];
//启动任务
[dataTask resume];
}
-(void)dataTask {
//设置请求地址
NSURL *url = [NSURL
URLWithString:@"https://api.seniverse.com/v3/weather/now.json?key=rj6wlseq27khpqrs&location=zunyi&language=zh-Hans&unit=c"];
//封装一个请求类
NSURLRequest *request = [NSURLRequest
requestWithURL:url];
//创建Session
NSURLSession *session = [NSURLSession
sharedSession];
//根据会话创建任务
NSURLSessionDataTask *dataTask = [session
dataTaskWithRequest:request
completionHandler:^(NSData *
_Nullable data, NSURLResponse *
_Nullable response, NSError *
_Nullable error) {
// 打印当前线程
NSLog(@"---%@---",[NSThread
currentThread]);
if (error ==
nil) {
}
/*
//回归到主线程的几种方法
1
[self performSelectorOnMainThread:(nonnull SEL) withObject:(nullable id) waitUntilDone:(BOOL)]
2
dispatch_async(dispatch_get_main_queue(), ^{
});
3
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
*/
}];
//启动任务
[dataTask resume];
}
/*
1、创建NSURLSession的会话
2、根据会话创建Task
3、执行Task
*/
不废话,直接上代码。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event
{
[self
post];
}
-(void)post {
//设置请求地址
NSURL *url = [NSURL
URLWithString:@"https://tieba.baidu.com/f"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:url];
//设置请求方式
request.HTTPMethod =
@"POST";
//设置请求体
request.HTTPBody = [@"kw=cos"
dataUsingEncoding:NSUTF8StringEncoding];
//创建Session
NSURLSession *session = [NSURLSession
sharedSession];
//根据会话创建任务
NSURLSessionDataTask *dataTask = [session
dataTaskWithRequest:request
completionHandler:^(NSData *
_Nullable data, NSURLResponse *
_Nullable response, NSError *
_Nullable error) {
// 打印当前线程
NSLog(@"---%@---",[NSThread
currentThread]);
if (error ==
nil) {
NSString *str = [[NSString
alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"---%@---",str);
//解析JSON数据,用字典接收
NSDictionary *dic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers
error:nil];
}
}];
//启动任务
[dataTask resume];
}
-(void)dataTask {
//设置请求地址
NSURL *url = [NSURL
URLWithString:@"https://api.seniverse.com/v3/weather/now.json?key=rj6wlseq27khpqrs&location=zunyi&language=zh-Hans&unit=c"];
//封装一个请求类
NSURLRequest *request = [NSURLRequest
requestWithURL:url];
//创建Session
NSURLSession *session = [NSURLSession
sharedSession];
//根据会话创建任务
NSURLSessionDataTask *dataTask = [session
dataTaskWithRequest:request
completionHandler:^(NSData *
_Nullable data, NSURLResponse *
_Nullable response, NSError *
_Nullable error) {
// 打印当前线程
NSLog(@"---%@---",[NSThread
currentThread]);
if (error ==
nil) {
}
/*
//回归到主线程的几种方法
1
[self performSelectorOnMainThread:(nonnull SEL) withObject:(nullable id) waitUntilDone:(BOOL)]
2
dispatch_async(dispatch_get_main_queue(), ^{
});
3
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
*/
}];
//启动任务
[dataTask resume];
}
相关文章推荐
- ios-程序中模拟GET和POST请求登录
- iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
- IOS开发之NSURL网络请求,同步异步,GET,POST
- IOS http请求的get 和 post的请求的区别
- IOS http请求的get 和 post的请求的区别
- iOS开发之网络编程篇二:Get,Post请求差异及用法
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- iOS开发 GET、POST请求方法:NSURLSession篇
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- iOS之同步请求、异步请求、GET请求、POST请求
- iOS开发网络篇—GET请求和POST请求
- iOS网络开发—POST请求和GET请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS 中使用 POST请求与GET请求的区别
- iOS- 网络请求的两种常用方式【GET & POST】的区别
- ios网络学习------1get post异步请求
- iOS陆哥开发笔记(二十七) (同步、异步请求, GET、POST请求)