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];