get请求

//得到session对象
NSURLSession *session = [NSURLSession sharedSession];

NSURL *url = [NSURL URLWithString:@"http://localhost:80/video"];

//创建一个任务
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"----%d", data.length);
}];

//开始任务
[task resume];

post请求

//创建session对象
NSURLSession *session = [NSURLSession sharedSession];

NSURL *url = [NSURL URLWithString:@"http://localhost:80/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//设置request
request.HTTPMethod = @"post";
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];

//创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"------%d", data.length);
}];

//开始任务
[task resume];

相关文章:

  • 2021-08-27
  • 2022-01-28
  • 2021-11-11
  • 2022-01-11
  • 2021-09-20
  • 2021-10-01
  • 2021-06-17
  • 2022-12-23
猜你喜欢
  • 2021-08-14
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案