#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self GETSession];
[self POSTSessin];
}
-(void)GETSession{
//GET 请求
//创建网络路径
NSURL *url = [NSURL URLWithString:@"http://www.biqumo.com/8_8230/4581301.html"];
//创建网络请求
NSURLRequest *requst = [NSURLRequest requestWithURL:url];
//获取会话对象
NSURLSession *session = [NSURLSession sharedSession];
//根据会话对象 创建一个task任务
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"请求对象成功");
//对服务器返回的数据进行处理
if (data && error == nil) {
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}else{
NSLog(@"请求失败");
NSLog(@"error:%@",error);
}
}];
//执行任务
[dataTask resume];
}
-(void)POSTSessin{
//创建网络路径
NSURL *url = [NSURL URLWithString:@"http://www.biqumo.com/8_8230/4581301.html"];
//创建网络请求
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url];
//设置请求方式
requst.HTTPMethod = @"POST";
//如果是post请求 参数附带信息要放到请求体
NSString *str = [NSString stringWithFormat:@"youname=%@&youpassword=%@"];
//将附带参数放到请求体
requst.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
//创建会话对象
NSURLSession *session = [NSURLSession sharedSession];
//根据会话对象创建会话任务
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"请求对象成功");
//对服务器返回的数据进行处理
if (data && error == nil) {
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}else{
NSLog(@"请求失败");
NSLog(@"error:%@",error);
}
}];
[dataTask resume];
}
@end
NSURLSessin
最新推荐文章于 2021-03-13 11:09:42 发布