网络请求的时候最常用到的就是get,post和upload了。
@interface UploadParam : NSObject
/**
* 图片的二进制数据
*/
@property (nonatomic, strong) NSData *data;
/**
* 服务器对应的参数名称
*/
@property (nonatomic, copy) NSString *name;
/**
* 文件的名称(上传到服务器后,服务器保存的文件名)
*/
@property (nonatomic, copy) NSString *filename;
/**
* 文件的MIME类型(image/png,image/jpg等)
*/
@property (nonatomic, copy) NSString *mimeType;
@end
@class UploadParam;
/**
* 网络请求类型
*/
typedef NS_ENUM(NSUInteger,HttpRequestType) {
/**
* get请求
*/
HttpRequestTypeGet = 0,
/**
* post请求
*/
HttpRequestTypePost
};
@interface HttpRequest : NSObject
/**
* 发送get请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)getWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 发送post请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)postWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 发送网络请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param type 请求的类型
* @param resultBlock 请求的结果
*/
+ (void)requestWithURLString:(NSString *)URLString
parameters:(id)parameters
type:(HttpRequestType)type
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 上传图片
*
* @param URLString 上传图片的网址字符串
* @param parameters 上传图片的参数
* @param uploadParam 上传图片的信息
* @param success 上传成功的回调
* @param failure 上传失败的回调
*/
+ (void)uploadWithURLString:(NSString *)URLString
parameters:(id)parameters
uploadParam:(UploadParam *)uploadParam
success:(void (^)())success
failure:(void (^)(NSError *error))failure;
#import "HttpRequest.h"
#import "AFNetworking.h"
#import "UploadParam.h"
@implementation HttpRequest
#pragma mark -- GET请求 --
+ (void)getWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
/** * 可以接受的类型 */
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
/** * 请求队列的最大并发数 */// manager.operationQueue.maxConcurrentOperationCount = 5;
/** * 请求超时的时间 *///
manager.requestSerializer.timeoutInterval = 5;
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }];}
#pragma mark -- POST请求 --
+ (void)postWithURLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }];}#pragma mark -- POST/GET网络请求 --+ (void)requestWithURLString:(NSString *)URLString parameters:(id)parameters type:(HttpRequestType)type success:(void (^)(id))success failure:(void (^)(NSError *))failure { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; switch (type) { case HttpRequestTypeGet: { [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; } break; case HttpRequestTypePost: { [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (failure) { failure(error); } }]; } break; }}
#pragma mark -- 上传图片 --
+ (void)uploadWithURLString:(NSString *)URLString parameters:(id)parameters uploadParam:(UploadParam *)uploadParam success:(void (^)())success failure:(void (^)(NSError *))failure { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id_Nonnull formData) {
[formData appendPartWithFileData:uploadParam.data name:uploadParam.name fileName:uploadParam.filename mimeType:uploadParam.mimeType];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}