使用NSURLSessionDataTask发送get和post请求

NSURLSessionDataTask是iOS7以后才会有的,取代NSURLConnection

参考:http://blog.csdn.net/ttf1993/article/details/46491113

1.基本使用

//GET请求(默认)

//创建session对象

NSURLSession *session = [NSURLSession sharedSession];

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

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//创建一个任务

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:8080/TFServer/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];


//2.例如下载图片

- (NSURLSessionDataTask *)downloadImageForURL:(NSURL *)imageURL canonicalURL:(NSURL *)canonicalURL completion:(void (^)(UIImage *))completion 

{

//canonicalURL为文件系统url,有时候可不要

since `imageURL` might be a filesystem URL from the local cache.有时候可不考虑

    NSURLSessionDataTask *dataTask = nil;//创建任务,先置空

    if (imageURL.absoluteString.length) {

        NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];

        if (request == nil) {

            dispatch_async(dispatch_get_main_queue(), ^{

                if (completion) {

                    completion(nil);

                }

            });

        }

        else {

            //1 创建对象

            NSURLSession *sesh = [NSURLSession sharedSession];

#if 0 //POST请求(默认为GET请求)

            //设置request

            request.HTTPMethod = @"post";

            request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];

#endif

            // 创建任务

            dataTask = [sesh dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//下载图片

                    NSError *error;

                    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL options:NSDataReadingMappedAlways error:&error]];

                    dispatch_async(dispatch_get_main_queue(), ^{

                        if (completion) {

                            completion(image);

                        }

                    }); 

                });

            }];

            //3 开始请求任务

            [dataTask resume];

        }

    }

    return dataTask;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值