網(wǎng)絡(luò)請求方式:Delete
Delete方法就是通過http請求刪除指定的URL上的資源啦臼朗,Delete請求一般會返回3種狀態(tài)碼:
200 (OK) - 刪除成功,同時返回已經(jīng)刪除的資源
202 (Accepted) - 刪除請求已經(jīng)接受蝎土,但沒有被立即執(zhí)行(資源也許已經(jīng)被轉(zhuǎn)移到了待刪除區(qū)域)
204 (No Content) - 刪除請求已經(jīng)被執(zhí)行视哑,但是沒有返回資源(也許是請求刪除不存在的資源造成的)
首先采用系統(tǒng)的NSURLConnection的方式進(jìn)行delete方式請求 // 指定一個網(wǎng)址 NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"; // 把網(wǎng)址轉(zhuǎn)換成NSURL NSURL *url = [NSURL URLWithString:str]; // 創(chuàng)建一個請求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 把請求方式設(shè)置成DELETE,默認(rèn)是GET [request setHTTPMethod:@"DELETE"]; // 進(jìn)行一步請求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // 對返回的數(shù)據(jù)進(jìn)行JSON解析 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // 打印結(jié)果 NSLog(@"%@", dic); }];
采用AFN進(jìn)行delete數(shù)據(jù)請求: AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager]; // 采用默認(rèn)的DELETE請求方式 [manage DELETE:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // 打印返回的結(jié)果 NSLog(@"%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 打印錯誤信息 NSLog(@"%@", error); }];
采用NSURLSession的方式進(jìn)行請求: NSString *strURL = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"; NSURL *url = [NSURL URLWithString:strURL]; NSMutableURLRequest *reqest = [NSMutableURLRequest requestWithURL:url]; [reqest setHTTPMethod:@"DELETE"]; // 創(chuàng)建一個session對象 NSURLSession *session = [NSURLSession sharedSession]; // 創(chuàng)建一個任務(wù) NSURLSessionTask *task = [session dataTaskWithRequest:reqest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@", dic); }]; // 執(zhí)行任務(wù) [task resume];
到這大家應(yīng)該發(fā)現(xiàn),DELETE和正常的GET請求沒有區(qū)別,只不過要制定一下請求方式,在日常的工作當(dāng)中,DELETE并不常用