TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
1、Get请求- // Get请求
- // 初始化网络引擎对象
- MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
-
- // 创建一个Get请求
- MKNetworkOperation *op = [engine operationWithPath:@"login.php?userid=admin&userpwd=123" params:nil httpMethod:@"GET"];
-
- // 设置Get请求处理方式
- [op onCompletion:^(MKNetworkOperation *operation){ // 请求成功
- NSLog(@"request string: %@",[operation responseString]);
-
- } onError:^(NSError *error){ // 请求失败
-
- NSLog(@"%@", error);
-
- }];
- // 入列操作(发起网络请求)
- [engine enqueueOperation:op];
-
复制代码 2、Post请求- // Post请求
- // 初始化网络引擎对象
- MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
-
- // 创建请求参数
- NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- [dic setValue:@"admin" forKey:@"username"];
- [dic setValue:@"123" forKey:@"password"];
-
- // 创建一个Post请求
- MKNetworkOperation *op = [engine operationWithPath:@"user.do" params:dic httpMethod:@"POST"];
-
- // 创建Post请求处理方式
- [op onCompletion:^(MKNetworkOperation *operation){
-
- NSLog(@"post response string :%@",[operation responseString]);
-
- } onError:^(NSError *error) {
-
- NSLog(@"%@", error);
- }];
-
- // 入列操作(发起网络请求)
- [engine enqueueOperation:op];
复制代码 3、文件上传- // 上传操作
- // 初始化网络引擎对象
- MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
-
- // 创建请求参数
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"admin", @"username", @"123", @"password",nil]
-
- // 创建一个Post请求
- MKNetworkOperation *op = [engine operationWithPath:@"upload" params:dict httpMethod:@"POST"];
-
- // 在Post请求中附件文件及指定文件类型
- NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SampleImage.jpg"];
- NSString *fileType = @"jpg";
- [op addFile:filePath forKey:@"media" mimeType:fileType];
-
- // 设置冻结属性,可在恢复网络时自动上传
- [op setFreezable:YES];
-
- // 设置上传处理方式
- [op addCompletionHandler:^(MKNetworkOperation* completedOperation) {
-
- NSString *responseString = [completedOperation responseString];
- NSLog(@"server response: %@",responseString);
-
- } errorHandler:^(MKNetworkOperation *errorOp, NSError* err){
-
- NSLog(@"Upload file error: %@", err);
-
- }];
-
- // 入列操作(发起网络请求)
- [engine enqueueOperation:op];
-
- // 上传进度
- [op onUploadProgressChanged:^(double progress) {
-
- DLog(@"Upload file progress: %.2f", progress*100.0);
- }];
复制代码
|
|