{"id":493,"date":"2014-11-03T10:46:23","date_gmt":"2014-11-03T02:46:23","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=493"},"modified":"2016-07-15T13:33:52","modified_gmt":"2016-07-15T05:33:52","slug":"mknetworkkit%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95%ef%bc%88get%e8%af%b7%e6%b1%82%e3%80%81post%e8%af%b7%e6%b1%82%e3%80%81%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/11\/03\/104623493.html","title":{"rendered":"MKNetworkKit\u7684\u57fa\u672c\u7528\u6cd5\uff08Get\u8bf7\u6c42\u3001Post\u8bf7\u6c42\u3001\u6587\u4ef6\u4e0a\u4f20\uff09"},"content":{"rendered":"<p>1\u3001Get\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/ Get\u8bf7\u6c42\n        \/\/ \u521d\u59cb\u5316\u7f51\u7edc\u5f15\u64ce\u5bf9\u8c61\n        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@\"192.168.2.176:9502\/api\" customHeaderFields:nil];\n        \n        \/\/ \u521b\u5efa\u4e00\u4e2aGet\u8bf7\u6c42\n        MKNetworkOperation *op = [engine operationWithPath:@\"login.php?userid=admin&userpwd=123\" params:nil httpMethod:@\"GET\"];\n        \n        \/\/ \u8bbe\u7f6eGet\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f\n        [op onCompletion:^(MKNetworkOperation *operation){        \/\/ \u8bf7\u6c42\u6210\u529f\n\n        NSLog(@\"request string: %@\",[operation responseString]);\n        \n    } onError:^(NSError *error){        \/\/ \u8bf7\u6c42\u5931\u8d25\n    \n                    NSLog(@\"%@\", error);\n    \n    }];\n\n        \/\/ \u5165\u5217\u64cd\u4f5c\uff08\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff09\n        [engine enqueueOperation:op];\n<\/pre>\n<p>2\u3001Post\u8bf7\u6c42<!--more--><\/p>\n<pre lang=\"objc\" line=\"1\">\/\/ Post\u8bf7\u6c42\n        \/\/ \u521d\u59cb\u5316\u7f51\u7edc\u5f15\u64ce\u5bf9\u8c61\n        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@\"192.168.2.176:9502\/api\" customHeaderFields:nil];\n        \n        \/\/ \u521b\u5efa\u8bf7\u6c42\u53c2\u6570\n        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];\n        [dic setValue:@\"admin\" forKey:@\"username\"];\n        [dic setValue:@\"123\" forKey:@\"password\"];\n   \n  \/\/ \u521b\u5efa\u4e00\u4e2aPost\u8bf7\u6c42\n        MKNetworkOperation *op = [engine operationWithPath:@\"user.do\" params:dic httpMethod:@\"POST\"];\n  \n  \/\/ \u521b\u5efaPost\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f\n        [op onCompletion:^(MKNetworkOperation *operation){\n                \n        NSLog(@\"post response string :%@\",[operation responseString]);  \n                      \n    } onError:^(NSError *error) {\n            \n        NSLog(@\"%@\", error);\n    }];\n    \n  \/\/ \u5165\u5217\u64cd\u4f5c\uff08\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff09 \n        [engine enqueueOperation:op];\n<\/pre>\n<p>3\u3001\u6587\u4ef6\u4e0a\u4f20<\/p>\n<pre lang=\"objc\" line=\"1\">  \/\/ \u4e0a\u4f20\u64cd\u4f5c\n  \/\/ \u521d\u59cb\u5316\u7f51\u7edc\u5f15\u64ce\u5bf9\u8c61\n        MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@\"192.168.2.176:9502\/api\" customHeaderFields:nil];\n        \n        \/\/ \u521b\u5efa\u8bf7\u6c42\u53c2\u6570\n        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@\"admin\", @\"username\", @\"123\", @\"password\",nil]\n  \n  \/\/ \u521b\u5efa\u4e00\u4e2aPost\u8bf7\u6c42        \n        MKNetworkOperation *op = [engine operationWithPath:@\"upload\" params:dict httpMethod:@\"POST\"];  \n  \n  \/\/ \u5728Post\u8bf7\u6c42\u4e2d\u9644\u4ef6\u6587\u4ef6\u53ca\u6307\u5b9a\u6587\u4ef6\u7c7b\u578b\n        NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@\"SampleImage.jpg\"]; \n        NSString *fileType = @\"jpg\";  \n        [op addFile:filePath forKey:@\"media\" mimeType:fileType];  \n      \n        \/\/ \u8bbe\u7f6e\u51bb\u7ed3\u5c5e\u6027\uff0c\u53ef\u5728\u6062\u590d\u7f51\u7edc\u65f6\u81ea\u52a8\u4e0a\u4f20  \n        [op setFreezable:YES];  \n  \n  \/\/ \u8bbe\u7f6e\u4e0a\u4f20\u5904\u7406\u65b9\u5f0f\n        [op addCompletionHandler:^(MKNetworkOperation* completedOperation) {  \n          \n                        NSString *responseString = [completedOperation responseString];  \n                        NSLog(@\"server response: %@\",responseString);  \n                        \n        } errorHandler:^(MKNetworkOperation *errorOp, NSError* err){  \n                \n                        NSLog(@\"Upload file error: %@\", err);  \n                        \n        }];  \n  \n  \/\/ \u5165\u5217\u64cd\u4f5c\uff08\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff09\n        [engine enqueueOperation:op];  \n  \n  \/\/ \u4e0a\u4f20\u8fdb\u5ea6\n        [op onUploadProgressChanged:^(double progress) {  \n                          \n                        DLog(@\"Upload file progress: %.2f\", progress*100.0);  \n        }];\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001Get\u8bf7\u6c42 \/\/ Get\u8bf7\u6c42 \/\/ \u521d\u59cb\u5316\u7f51\u7edc\u5f15\u64ce\u5bf9\u8c61 MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@&#8221;192.168.2.176:9502\/api&#8221; customHeaderFields:nil]; \/\/ \u521b\u5efa\u4e00\u4e2aGet\u8bf7\u6c42 MKNetworkOperation *op = [engine operationWithPath:@&#8221;login.php?userid=admin&#038;userpwd=123&#8243; params:nil httpMethod:@&#8221;GET&#8221;]; \/\/ \u8bbe\u7f6eGet\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f [op onCompletion:^(MKNetworkOperation *operation){ \/\/ \u8bf7\u6c42\u6210\u529f NSLog(@&#8221;request string: %@&#8221;,[operation responseString]); } onError:^(NSError *error){ \/\/ \u8bf7\u6c42\u5931\u8d25 NSLog(@&#8221;%@&#8221;, error); }]; \/\/ \u5165\u5217\u64cd\u4f5c\uff08\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\uff09 [engine enqueueOperation:op]; 2\u3001Post\u8bf7\u6c42<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[17,18],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-project","tag-skill","tag-network"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}