//
// HttpTool.h
// AFNetWorking 3.0 工具类
//
// Created by peony on 18/1/21.
// Copyright (c) 2018年 zsl. All rights reserved.
//
#import
@class FormData;
@interfaceHttpTool :NSObject
/**
* 发送一个GET请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)get:(NSString*)url params:(NSDictionary*)params session:(NSString*)session success:(void(^)(idjson))success failure:(void(^)(NSError*error))failure;
/**
* 发送一个POST请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)post:(NSString*)url params:(NSDictionary*)params session:(NSString*)session success:(void(^)(idjson))success failure:(void(^)(NSError*error))failure;
/**
* 发送一个POST文件请求
*
* @param url 请求路径
* @param params 请求参数
* @param dataSource 文件参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)post:(NSString*)url params:(NSDictionary*)params session:(NSString*)session dataSource:(FormData*)dataSource success:(void(^)(idjson))success failure:(void(^)(NSError*error))failure;
/**
多张图片上传
@param url 接口
@param params 参数
@param images 图片数组
@param success 成功状态
@param failure 失败状态
*/
+ (void)postMoreDataWithUrl:(NSString*)url Params:(NSDictionary*)params ImageParams:(NSString*)imageParams Session:(NSString*)session ImageArray:(NSMutableArray*)images Success:(void(^)(idjson))success Failure:(void(^)(NSError*error))failure;
@end
/**
* 用来封装文件数据的模型
*/
@interfaceFormData :NSObject
/**
* 文件数据
*/
@property(nonatomic, strong)NSData * data;
/**
* 参数名
*/
@property(nonatomic, copy)NSString *paramsName;
/**
* 文件名
*/
@property(nonatomic, copy)NSString *filename;
/**
* 文件类型
*/
@property(nonatomic, copy)NSString *mimeType;
@end
//
// HttpTool.h
// AFNetWorking 3.0 工具类
//
// Created by peony on 15/7/21.
// Copyright (c) 2015年 zsl. All rights reserved.
//
#import "HttpTool.h"
#import "AFNetworking.h"
@interface HttpTool ()
@end
@implementation HttpTool
/**
* 发送一个GET文件请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)get:(NSString*)url params:(NSDictionary*)params session:(NSString*)session success:(void(^)(id))success failure:(void(^)(NSError*))failure {
// 1.获得请求管理者
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr.requestSerializer setValue:session forHTTPHeaderField:@"Cookie"];
//[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
mgr.requestSerializer = [AFJSONRequestSerializer serializer];
//申明返回的结果是json类型
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
// 2.发送GET请求
[mgrGET:urlparameters:paramsprogress:^(NSProgress*_NonnulldownloadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSString*cookieStr = response.allHeaderFields[COOKIE];
if(cookieStr !=nil) {
// 根据cookie中的内容字符串用“; ”来分割出cookie
NSArray*result = [cookieStrcomponentsSeparatedByString:@"; "];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
OMGLog(@"cookieStr=======>%@",result[0]);
[userDefaultssetObject:result[0]forKey:COOKIE];
[userDefaultssynchronize];
}
if(success) {
success(responseObject);
}
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
if(failure) {
failure(error);
[SVProgressHUD showErrorWithStatus:@"连接服务器超时"];
}
}];
}
/**
* 发送一个POST请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)post:(NSString*)url params:(NSDictionary*)params session:(NSString*)session success:(void(^)(id))success failure:(void(^)(NSError*))failure {
// 1.获得请求管理者
AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
[mgr.requestSerializer setValue:session forHTTPHeaderField:@"Cookie"];
[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
mgr.requestSerializer = [AFJSONRequestSerializer serializer];
//申明返回的结果是json类型
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
// 2.发送POST请求
[mgrPOST:urlparameters:paramsprogress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSHTTPURLResponse * response = (NSHTTPURLResponse *)task.response;
NSString*cookieStr = response.allHeaderFields[COOKIE];
if(cookieStr !=nil) {
NSArray*result = [cookieStrcomponentsSeparatedByString:@"; "];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
OMGLog(@"cookieStr=======>%@",result[0]);
[userDefaultssetObject:result[0]forKey:COOKIE];
}
if(success) {
success(responseObject);
}
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
if(failure) {
failure(error);
[SVProgressHUD showErrorWithStatus:@"连接服务器超时"];
}
}];
}
/**
* 发送单个POST文件请求
*
* @param url 请求路径
* @param params 请求参数
* @param dataSource 文件参数
* @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
* @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
+ (void)post:(NSString*)url params:(NSDictionary*)params session:(NSString*)session dataSource:(FormData*)dataSource success:(void(^)(id))success failure:(void(^)(NSError*))failure {
// 1.获得请求管理者
AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
[mgr.requestSerializer setValue:session forHTTPHeaderField:@"Cookie"];
[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
mgr.requestSerializer = [AFJSONRequestSerializer serializer];
//申明返回的结果是json类型
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
// 2.请求内容
[mgrPOST:url parameters:params constructingBodyWithBlock:^(id _Nonnull formData) {
[formDataappendPartWithFileData:dataSource.data name:dataSource.paramsName fileName:dataSource.filename mimeType:dataSource.mimeType];
}progress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSHTTPURLResponse * response = (NSHTTPURLResponse *)task.response;
NSString*cookieStr = response.allHeaderFields[COOKIE];
if(cookieStr !=nil) {
NSArray*result = [cookieStrcomponentsSeparatedByString:@"; "];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
OMGLog(@"cookieStr=======>%@",result[0]);
[userDefaultssetObject:result[0]forKey:COOKIE];
}
if(success) {
success(responseObject);
}
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
if(failure) {
failure(error);
[SVProgressHUD showErrorWithStatus:@"服务器异常"];
}
}];
}
/**
多张图片上传
@param url 接口
@param params 参数
@param images 图片数组
@param success 成功状态
@param failure 失败状态
*/
+ (void)postMoreDataWithUrl:(NSString*)url Params:(NSDictionary*)params ImageParams:(NSString*)imageParams Session:(NSString*)session ImageArray:(NSMutableArray*)images Success:(void(^)(idjson))success Failure:(void(^)(NSError*error))failure {
AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
[mgr.requestSerializer setValue:session forHTTPHeaderField:@"Cookie"];
[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
mgr.requestSerializer = [AFJSONRequestSerializer serializer];
//申明返回的结果是json类型
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
[mgrPOST:url parameters:params constructingBodyWithBlock:^(id _Nonnull formData) {
for(UIImage*imageinimages) {
// 图片太大会上传不到服务器上面去
NSData*imageData =UIImageJPEGRepresentation(image,0.1);;
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat =@"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"files"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
[formData appendPartWithFileData:imageData name:imageParams fileName:fileName mimeType:@"image/jpeg"];
}
} progress:^(NSProgress *_NonnulluploadProgress) {
} success:^(NSURLSessionDataTask *_Nonnulltask,id _NullableresponseObject) {
NSHTTPURLResponse * response = (NSHTTPURLResponse *)task.response;
NSString *cookieStr = response.allHeaderFields[COOKIE];
if(cookieStr !=nil) {
NSArray *result = [cookieStr componentsSeparatedByString:@"; "];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
OMGLog(@"cookieStr=======>%@",result[0]);
[userDefaults setObject:result[0] forKey:COOKIE];
}
success(responseObject);
} failure:^(NSURLSessionDataTask *_Nullabletask, NSError *_Nonnullerror) {
failure(error);
}];
}
@end
/**
* 用来封装文件数据的模型
*/
@implementation FormData
@end
网友评论