您的位置:首页 > 理论基础 > 计算机网络

iOS 网络请求(POST/GET) 系统、AFNetworking,MKNetwork

2015-05-15 23:08 489 查看


1. iOS 系统请求

(1) POST:同步

NSString* requestUrlString = kAPI;
NSURL *baseURL = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
[request setHTTPMethod:@"POST"];
NSString *postString =@"timestamp=参数1&channel=参数2&product=参数3...";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
request.timeoutInterval = 30;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSURL *baseURL = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
[request setHTTPMethod:@"GET"];
request.timeoutInterval = 30;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

(2) GET:同步

NSURL *baseURL = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
[request setHTTPMethod:@"GET"];
request.timeoutInterval = 30;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


2.AFNetworking

(1) POST:异步

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"参数1",timestamp,@"参数2",channel,@"参数3",product,... nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:api parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//成功处理
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败处理
}];


(2) GET:异步

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"参数1",timestamp,@"参数2",channel,@"参数3",product,... nil]
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:api parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//成功处理
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败处理
}];


3.MKNetworking

(1)POST:异步

MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:URL_API];
MKNetworkOperation *op = [engine operationWithURLString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] params:params httpMethod:@"POST"];
[op addData:data forKey:@"imgFile" mimeType:@"multipart/form-data" fileName:@"thumbnail.png"];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
//成功
} onError:^(NSError *error) {
//失败
}];
[engine enqueueOperation:op];

(2) GET:异步

MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:URL_API];
MKNetworkOperation *op = [engine operationWithURLString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] params:params httpMethod:@"GET"];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
//成功
} onError:^(NSError *error) {
//失败
}];
[engine enqueueOperation:op];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 异步 网络