//需要AFN
//.h
//AFNetworking
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result;
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_HEADER:(NSString *)url result:(void(^)(id responseObject,NSError *error))result;
+ (void)get_HEADER:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_HEADER:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result;
+ (void)post_HEADER:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result;
//NSURLConnection
//get
+ (void)get_URLConnection:(NSString *)urlString result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result;
//post
+ (void)post_URLConnection:(NSString *)urlString parameters:(NSDictionary *)parameters result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result;
//NSURLConnection curl url请求(header)
//get
+ (void)get_URLConnection:(NSString *)urlString token:(NSString *)token result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result;
//post
+ (void)post_URLConnection:(NSString *)urlString parameters:(NSDictionary *)parameters token:(NSString *)token result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result;
#pragma mark --- 网络请求 Data -> NSDictionary
+ (NSDictionary *)mzNET:(NSData *)date;
+ (NSDictionary *)mzNET:(NSData *)date error:(NSError *)error;
//.m
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result {
[self post:url parameters:parameters contentType:nil result:result];
}
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self post:url parameters:parameters contentType:nil progress:progress result:result];
}
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self post:url parameters:parameters contentType:contentType progress:nil result:result];
}
+ (void)post:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
if (contentType) {
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:contentType];
}
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
+ (void)get:(NSString *)url result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:nil contentType:nil result:result];
}
+ (void)get:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:nil contentType:nil progress:progress result:result];
}
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:parameters contentType:nil result:result];
}
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:parameters contentType:nil progress:progress result:result];
}
+ (void)get:(NSString *)url contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:nil contentType:contentType result:result];
}
+ (void)get:(NSString *)url contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:nil contentType:contentType progress:progress result:result];
}
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self get:url parameters:parameters contentType:contentType progress:nil result:result];
}
+ (void)get:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
if (contentType) {
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:contentType];
}
NSDictionary *par = nil;
if (parameters) {
par = parameters;
}
[manager GET:url parameters:par progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
+ (void)get_JSON:(NSString *)url result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:nil contentType:nil result:result];
}
+ (void)get_JSON:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:nil contentType:nil progress:progress result:result];
}
+ (void)get_JSON:(NSString *)url contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:nil contentType:contentType result:result];
}
+ (void)get_JSON:(NSString *)url contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:nil contentType:contentType progress:progress result:result];
}
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:parameters contentType:nil result:result];
}
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:parameters contentType:nil progress:progress result:result];
}
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self get_JSON:url parameters:parameters contentType:contentType progress:nil result:result];
}
+ (void)get_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:contentType];
NSDictionary *par = nil;
if (parameters) {
par = parameters;
}
[manager GET:url parameters:par progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result {
[self post_JSON:url parameters:parameters contentType:nil result:result];
}
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
[self post_JSON:url parameters:parameters contentType:nil progress:progress result:result];
}
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType result:(void(^)(id responseObject,NSError *error))result {
[self post_JSON:url parameters:parameters contentType:contentType progress:nil result:result];
}
+ (void)post_JSON:(NSString *)url parameters:(NSDictionary *)parameters contentType:(NSString *)contentType progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:contentType];
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
+ (void)get_HEADER:(NSString *)url result:(void(^)(id responseObject,NSError *error))result {
[self get_HEADER:url progress:nil result:result];
}
+ (void)get_HEADER:(NSString *)url progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
+ (void)post_HEADER:(NSString *)url parameters:(NSDictionary *)parameters result:(void(^)(id responseObject,NSError *error))result {
[self post_HEADER:url parameters:parameters progress:nil result:result];
}
+ (void)post_HEADER:(NSString *)url parameters:(NSDictionary *)parameters progress:(void(^)(NSProgress *))progress result:(void(^)(id responseObject,NSError *error))result {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
if (downloadProgress) {
if (progress) {
progress(downloadProgress);
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (result) {
result(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (result) {
result(nil,error);
}
}];
}
//NSURLConnection
//get
+ (void)get_URLConnection:(NSString *)urlString result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result {
[self get_URLConnection:urlString token:nil result:result];
}
//post
+ (void)post_URLConnection:(NSString *)urlString parameters:(NSDictionary *)parameters result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result {
[self post_URLConnection:urlString parameters:parameters token:nil result:result];
}
//NSURLConnection curl url请求(header)
//get
+ (void)get_URLConnection:(NSString *)urlString token:(NSString *)token result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL:url];
if (token) {
NSString *value = [NSString stringWithFormat:@"bearer %@",token];
[request setValue:value forHTTPHeaderField:@"Authorization"];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data != nil && connectionError == nil) {
NSDictionary *userInfo = [self mzNET:data];
if (result) {
result(userInfo,nil);
}
}else if (data != nil && connectionError != nil ) {
NSDictionary *userInfo = [self mzNET:data];
if (result) {
result(userInfo,connectionError);
}
}else if (data == nil && connectionError != nil) {
if (result) {
result(nil,connectionError);
}
}else {
if (result) {
result(nil,nil);
}
}
}];
}
//post
+ (void)post_URLConnection:(NSString *)urlString parameters:(NSDictionary *)parameters token:(NSString *)token result:(void(^)(NSDictionary *userInfo,NSError *connectionError))result {
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:10];
//3.设置表单提交的方法(默认为get)
[request setHTTPMethod:@"post"];
//4.设置要提交的参数
// =[NSString stringWithFormat:@"uname=%@&upas=%@&btn=login",uname,upas];
if (token) {
NSString *value = [NSString stringWithFormat:@"bearer %@",token];
[request setValue:value forHTTPHeaderField:@"Authorization"];
}
if (parameters) {
NSArray *valueArr = parameters.allValues;
NSArray *keyArr = parameters.allKeys;
NSString *bodyStr = nil;
for (int i = 0; i < valueArr.count && i < keyArr.count; i ++) {
if (i == 0) {
bodyStr = [NSString stringWithFormat:@"%@=%@",[keyArr firstObject],[valueArr firstObject]];
}else {
bodyStr = [bodyStr stringByAppendingString:[NSString stringWithFormat:@"&%@=%@",[keyArr firstObject],[valueArr firstObject]]];
}
}
[request setHTTPBody:[bodyStr dataUsingEncoding:NSUTF8StringEncoding]];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data != nil && connectionError == nil) {
NSDictionary *userInfo = [self mzNET:data];
if (result) {
result(userInfo,nil);
}
}else if (data != nil && connectionError != nil ) {
NSDictionary *userInfo = [self mzNET:data];
if (result) {
result(userInfo,connectionError);
}
}else if (data == nil && connectionError != nil) {
if (result) {
result(nil,connectionError);
}
}else {
if (result) {
result(nil,nil);
}
}
}];
}
#pragma mark --- 网络请求 Data -> NSDictionary
+ (NSDictionary *)mzNET:(NSData *)date {
NSDictionary *mzDic = [NSJSONSerialization JSONObjectWithData:date options:NSJSONReadingMutableContainers error:nil];
return mzDic;
}
+ (NSDictionary *)mzNET:(NSData *)date error:(NSError *)error{
NSDictionary *mzDic = [NSJSONSerialization JSONObjectWithData:date options:NSJSONReadingMutableContainers error:&error];
return mzDic;
}
谢谢阅读!