object-c http-get-post

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];

猜你喜欢

转载自www.cnblogs.com/manhelp/p/11918731.html