使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章