1 //2 // RootViewController.m3 // webTest4 //5 // Created by mmc on 13-11-24.6 // Copyright (c) 2013年 mmc. All rights reserved.7 //89 #import \"RootViewController.h\"10 #import \"MKNetworkEngine.h\"1112 @implementation RootViewController1314 - (IBAction) getTest:(id)sender15 {16 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@\"192.168.1.105:8080\"];17 [engine useCache];1819 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0];20 [params setObject:@\"get数据1\" forKey:@\"arg1\"];21 [params setObject:@\"get数据2\" forKey:@\"arg2\"];2223 //最后的斜杠不能丢掉,不然会出问题24 MKNetworkOperation *operation = [engine operationWithPath:@\"/yii/testApp/index.php?r=httpTest/getTest/\"25 params:params26 httpMethod:@\"GET\"];2728 [operation addCompletionHandler:^(MKNetworkOperation *completedOperation)29 {30 NSString *responseString = [completedOperation responseString];31 NSLog(@\"%@\", responseString);3233 if([completedOperation isCachedResponse]) {34 NSLog(@\"Data from cache %@\", [completedOperation responseString]);35 }36 else {37 NSLog(@\"Data from server %@\", [completedOperation responseString]);38 }3940 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {4142 NSLog(@\"%@\",error);43 }];4445 [engine enqueueOperation:operation];4647 }4849 - (IBAction) postTest:(id)sender50 {51 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@\"192.168.1.105:8080\"];52 [engine useCache];5354 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0];55 [params setObject:@\"post数据1\" forKey:@\"arg1\"];56 [params setObject:@\"post数据2\" forKey:@\"arg2\"];5758 MKNetworkOperation *operation = [engine operationWithPath:@\"/yii/testApp/index.php?r=httpTest/postTest/\"59 params:params60 httpMethod:@\"POST\"];6162 [operation addCompletionHandler:^(MKNetworkOperation *completedOperation)63 {64 NSString *responseString = [completedOperation responseString];65 NSLog(@\"%@\", responseString);6667 if([completedOperation isCachedResponse]) {68 NSLog(@\"Data from cache %@\", [completedOperation responseString]);69 }70 else {71 NSLog(@\"Data from server %@\", [completedOperation responseString]);72 }7374 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {7576 NSLog(@\"%@\",error);77 }];7879 [engine enqueueOperation:operation];80 }8182 - (IBAction) downloadTest:(id)sender83 {84 MKNetworkEngine* engine = [[MKNetworkEngine alloc] initWithHostName:@\"127.0.0.1\"];85 [engine useCache];8687 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);88 NSString *cachesDirectory = paths[0];89 NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:@\"x.iso\"];909192 //判断之前是否下载过 如果有下载重新构造Header93 NSMutableDictionary *newHeadersDict = [[NSMutableDictionary alloc] init];9495 NSFileManager *fileManager = [NSFileManager defaultManager];969798 if ([fileManager fileExistsAtPath:downloadPath])99 {100 NSError *error = nil;101 unsigned long long fileSize = [[fileManager attributesOfItemAtPath:downloadPath error:&error] fileSize];102103 NSString *headerRange = [NSString stringWithFormat:@\"bytes=%llu-\", fileSize];104 [newHeadersDict setObject:headerRange forKey:@\"Range\"];105 }106107 MKNetworkOperation *operation = [engine operationWithURLString:@\"http://192.168.1.105:8080/2.iso\"];108109 [operation addDownloadStream:[NSOutputStream outputStreamToFileAtPath:downloadPath110 append:YES]];111112 [operation addHeaders:newHeadersDict];113 [engine enqueueOperation:operation];114115 //进度回调116 [operation onDownloadProgressChanged:^(double progress)117 {118 NSLog(@\"download %.2f\", progress*100.0);119 }];120121 //结束回调122 [operation addCompletionHandler:^(MKNetworkOperation* completedRequest)123 {124 NSLog(@\"download complete %@\", completedRequest);125 }errorHandler:^(MKNetworkOperation *errorOp, NSError* error)126 {127 NSLog(@\"%@\", error);128 }];129 }130131 @end
- 点赞
- 收藏
- 分享
- 文章举报
GS-NICE发布了177 篇原创文章 · 获赞 0 · 访问量 2739私信关注