使用方法:
在需要使用的地方導(dǎo)入:#import"MKNetworkKit.h"
http方式:
GET請求:
[cpp]view plaincopy
MKNetworkEngine?*engine?=?[[MKNetworkEngine?alloc]?initWithHostName:@"192.168.2.176:3000"customHeaderFields:nil];
MKNetworkOperation?*op?=?[engine?operationWithPath:@"/index"params:nil?httpMethod:@"GET"ssl:NO];
[op?addCompletionHandler:^(MKNetworkOperation?*operation)?{
NSLog(@"[operation?responseData]-->>%@",?[operation?responseString]);
}errorHandler:^(MKNetworkOperation?*errorOp,?NSError*?err)?{
NSLog(@"MKNetwork?request?error?:?%@",?[err?localizedDescription]);
}];
[engine?enqueueOperation:op];
POST請求:
[cpp]view plaincopy
MKNetworkEngine?*engine?=?[[MKNetworkEngine?alloc]?initWithHostName:@"192.168.2.176:3000"customHeaderFields:nil];
NSMutableDictionary?*dic?=?[[NSMutableDictionary?alloc]?init];
[dic?setValue:@"admin"forKey:@"username"];
[dic?setValue:@"123"forKey:@"password"];
MKNetworkOperation?*op?=?[engine?operationWithPath:@"/login"params:dic?httpMethod:@"POST"];
[op?addCompletionHandler:^(MKNetworkOperation?*operation)?{
NSLog(@"[operation?responseData]-->>%@",?[operation?responseString]);
}errorHandler:^(MKNetworkOperation?*errorOp,?NSError*?err)?{
NSLog(@"MKNetwork?request?error?:?%@",?[err?localizedDescription]);
}];
[engine?enqueueOperation:op];
https方式:
[cpp]view plaincopy
#define?serverHost???????????????@"192.168.1.84:5558"
-?(NSDictionary?*)getDataFromURL:(NSString?*)Path?params:(NSDictionary?*)data
{
NSLog(@"MKNetwork?request?URL:??%@%@???\n?Data:?%@",serverHost,Path,data);
__block?NSDictionary?*responseJSON;
__block?NSError?*error?=?nil;
MKNetworkEngine?*engine?=?[[MKNetworkEngine?alloc]?initWithHostName:serverHost?customHeaderFields:nil];
MKNetworkOperation?*op?=?[engine?operationWithPath:Path??params:data?httpMethod:@"POST"ssl:YES];
//????在請求中添加證書
op.clientCertificate?=?[[[NSBundle?mainBundle]?resourcePath]?stringByAppendingPathComponent:@"client.p12"];
op.clientCertificatePassword?=?@"test";
//???當(dāng)服務(wù)器端證書不合法時(shí)是否繼續(xù)訪問
op.shouldContinueWithInvalidCertificate=YES;
[op?addCompletionHandler:^(MKNetworkOperation?*operation)?{
NSLog(@"[operation?responseData]-->>%@",?[operation?responseString]);
responseJSON=[NSJSONSerialization?JSONObjectWithData:[operation?responseData]?options:kNilOptions?error:&error];
if(error)?{
NSLog(@"JSONSerialization?failed!?-?error:?%@",?error);
};
error=nil;
}?errorHandler:^(MKNetworkOperation?*errorOp,?NSError*?err)?{
error=err;
}];
[engine?enqueueOperation:op];
while(!error&&!responseJSON){}
if(error)?{
NSLog(@"MKNetwork?request?error?:?%@",?error);
returnnil;
}
if(responseJSON){
NSLog(@"JSONSerialization?successed!?-?responseJSON:?%@",?responseJSON);
}
returnresponseJSON;
}
NSDictionary?*params=[NSDictionary?dictionaryWithObjectsAndKeys:@"admin",@"userName",?@"123",@"password",?nil];
NSDictionary?*responseDict?=?[self?getDataFromURL:@"/login"params:params];
下載文件:
[cpp]view plaincopy
+(MKNetworkOperation*)?downloadFatAssFileFrom:(NSString*)?remoteURL?toFile:(NSString*)?filePath?{
MKNetworkEngine?*engine?=?[[MKNetworkEngine?alloc]?initWithHostName:@"127.0.0.1:5558"customHeaderFields:nil];
MKNetworkOperation?*op?=?[engine?operationWithURLString:remoteURL
params:nil
httpMethod:@"GET"];
[op?addDownloadStream:[NSOutputStream?outputStreamToFileAtPath:filePath
append:YES]];
[engine?enqueueOperation:op];
returnop;
}
+(void)testDownload{
NSArray?*paths?=?NSSearchPathForDirectoriesInDomains(NSCachesDirectory,?NSUserDomainMask,?YES);
NSString?*cachesDirectory?=?[paths?objectAtIndex:0];
NSString?*downloadPath?=?[cachesDirectory?stringByAppendingPathComponent:@"DownloadedFile.pdf"];
MKNetworkOperation?*downloadOperation=[HttpManager?downloadFatAssFileFrom:@"http://127.0.0.1:5558/QQ"
toFile:downloadPath];
[downloadOperation?onDownloadProgressChanged:^(doubleprogress)?{
//下載進(jìn)度
NSLog(@"download?progress:?%.2f",?progress*100.0);
}];
//事件處理
[downloadOperation?addCompletionHandler:^(MKNetworkOperation*?completedRequest)?{
NSLog(@"download?file?finished!");
}??errorHandler:^(MKNetworkOperation?*errorOp,?NSError*?err)?{
NSLog(@"download?file?error:?%@",?err);
}];
}
上傳文件:
[cpp]view plaincopy
+(MKNetworkOperation*)?uploadImageFromFile:(NSString*)?filePath?mimeType:(NSString?*)fileType{
MKNetworkEngine?*engine?=?[[MKNetworkEngine?alloc]?initWithHostName:@"127.0.0.1:5558"customHeaderFields:nil];
MKNetworkOperation?*op?=?[engine?operationWithPath:@"upload"
params:[NSDictionary?dictionaryWithObjectsAndKeys:
@"admin",?@"username",
@"123",?@"password",nil]
httpMethod:@"POST"];
[op?addFile:filePath?forKey:@"media"mimeType:fileType];
//?setFreezable?uploads?your?images?after?connection?is?restored!
[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];
returnop;
}
+(void)testUpload{
NSString?*uploadPath?=?[[[NSBundle?mainBundle]?resourcePath]?stringByAppendingPathComponent:@"SampleImage.jpg"];
//????NSData?*myData?=?[NSData?dataWithContentsOfFile:uploadPath];
//????NSLog(@">>>>>>>>%@",myData);
MKNetworkOperation?*uploadOperation?=?[HttpManager?uploadImageFromFile:uploadPath?mimeType:@"jpg"];
[uploadOperation?onUploadProgressChanged:^(doubleprogress)?{
//????????上傳進(jìn)度
DLog(@"Upload?file?progress:?%.2f",?progress*100.0);
}];
}
上傳文件時(shí)服務(wù)器端程序(Node.Js):
[javascript]view plaincopy
varexpress?=?require('express')
,fs=require('fs');
varapp?=?module.exports?=?express.createServer();
//?Configuration
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
//?Routes
app.post('/upload',function(req,?res)?{
console.log(req);
vartmp_path?=?req.files.media.path;//?獲得文件的臨時(shí)路徑
vartarget_path?='./'+req.files.media.name;//?指定文件上傳后的目錄
fs.rename(tmp_path,?target_path,function(err)?{//?移動(dòng)文件
if(err)throwerr;
fs.unlink(tmp_path,function()?{//?刪除臨時(shí)文件夾文件,
if(err)throwerr;
res.send({server:'success'});
res.end();
});
});
});
app.listen(5558);