httpHelper请求辅助类
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
@interface AFHttpClient : NSObject
DEFINE_SINGLETON_FOR_HEADER(AFHttpClient);
/**
* 判断网络是否正常
*
* @return Bool YES 表示正常
*/
- (BOOL)isOnline;
/**
* 停止请求
*/
- (void)cancelAllRequest;
/**
* Get 请求
*
* @param path 路径
* @param parameters 参数
* @param success success block
* @param failure failure block
*/
- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
/**
* Get 请求
*
* @param url 路径
* @param parameter 参数
* @param completion completion block
*/
- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path
paramter:(NSDictionary *)parameters
completion:(void (^)(BOOL success))completion;
/**
* Post 请求
*
* @param path 路径
* @param parameters 参数
* @param success success block
* @param failure failure block
*/
- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
/**
* Post 请求 发送json数据到服务端
*
* @param path 路径
* @param parameters 参数
* @param success success description
* @param postHttpRequest postHttpRequest description
* @param path path description
* @param parameters parameters description
* @param completion completion description
*/
- (AFHTTPRequestOperation*)postJsonHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
/**
* Post 请求
*
* @param url 路径
* @param parameter 参数
* @param completion completion block
*/
- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path
paramter:(NSDictionary *)parameters
completion:(void (^)(BOOL success))completion;
/**
* 上传图片
*
* @param uploadPath 上传路径
* @param imagePath 文件路径
* @param parameters 参数
* @param success success block
* @param failure failure block
*/
- (AFHTTPRequestOperation *)uploadImage:(NSString *)uploadPath
imagePath:(NSString *)imagePath
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
/**
* 上传图片
*
* @param url 上传路径
* @param path 文件本地路径
* @param name 文件名
* @param parameter 参数
* @param completion block
*/
- (AFHTTPRequestOperation *)uploadImage:(NSString *)url
path:(NSString *)path
name:(NSString *)name
paramter:(NSDictionary *)parameter
completion:(void (^)(BOOL success))completion;
/**
* 上传图片
*
* @param url 上传路径
* @param imageData 图像数据
* @param imageName 图像名称
* @param fileName 文件名
* @param parameter 参数
* @param success success block
* @param failure failure block
*/
- (AFHTTPRequestOperation *)uploadImage:(NSString *)url
imageData:(NSData *)imageData
imageName:(NSString *)imageName
fileName:(NSString*)fileName
paramter:(NSDictionary *)parameter
success:(void (^)(AFHTTPRequestOperation *operation,id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))failure;
/**
* 下载图片保存文件
*
* @param url 地址
* @param path 文件名
* @param block block
*/
- (AFHTTPRequestOperation *)downloadImage:(NSString*)url
saveToFile:(NSString *)path
completion:(void (^)(bool success))block;
@end
.m文件
#import "AFHttpClient.h"
#import "AFNetworking.h"
#import "YQZTools.h"
#import "YQZSetting.h"
#import "Reachability.h"
#import "YQZViewController.h"
@interface AFHttpClient()
@property (nonatomic, strong) AFHTTPRequestOperationManager *currentRequest;
@end
@implementation AFHttpClient
DEFINE_SINGLETON_FOR_CLASS(AFHttpClient);
- (AFHTTPRequestOperationManager *)currentRequest
{
if (!_currentRequest) {
self.currentRequest = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:[YQZTools getProxy]]];
}
return _currentRequest;
}
- (BOOL)isOnline
{
return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable;
}
#pragma mark - 请求 -
- (void)cancelAllRequest
{
[self.currentRequest.operationQueue cancelAllOperations];
}
- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){
if (![[YQZSetting sharedInstance] getAlertViewShow]) {
[YQZTools alert:@"提示" message:@"您的网络不给力哦"];
failure(nil,nil);
}
return nil;
}
[self showRequestLogWithUrl:path parameter:parameters];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager GET:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
int code = [[responseObject objectForKey:@"resultCode"] intValue];
if (code == 399) //系统错误
{
failure(operation, nil);
return;
}
if (success)
{
success(operation, responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showErrorLog:error];
if (failure) {
failure(operation, error);
}
}];
}
- (AFHTTPRequestOperation *)getHttpRequest:(NSString *)path
paramter:(NSDictionary *)parameters
completion:(void (^)(BOOL success))completion
{
[self showRequestLogWithUrl:path parameter:parameters];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager GET:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
completion(YES);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showErrorLog:error];
completion(NO);
}];
}
- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){
if (![[YQZSetting sharedInstance] getAlertViewShow]) {
[YQZTools alert:@"提示" message:@"您的网络不给力哦"];
if (failure) {
failure(nil,nil);
}
}
return nil;
}
[self showRequestLogWithUrl:path parameter:parameters];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager POST:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
int code = [[responseObject objectForKey:@"resultCode"] intValue];
if (code == 399) //系统错误
{
failure(operation, nil);
return;
}
if (success)
{
success(operation, responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showErrorLog:error];
failure(operation, error);
}];
}
- (AFHTTPRequestOperation*)postJsonHttpRequest:(NSString *)path
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
if(NotReachable == [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]){
if (![[YQZSetting sharedInstance] getAlertViewShow]) {
[YQZTools alert:@"提示" message:@"您的网络不给力哦"];
failure(nil,nil);
}
return nil;
}
[self showRequestLogWithUrl:path parameter:parameters];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
return [manager POST:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
int code = [[responseObject objectForKey:@"resultCode"] intValue];
if (code == 399) //系统错误
{
failure(operation, nil);
return;
}
if (success)
{
success(operation, responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showErrorLog:error];
failure(operation, error);
}];
}
- (AFHTTPRequestOperation*)postHttpRequest:(NSString *)path
paramter:(NSDictionary *)parameters
completion:(void (^)(BOOL success))completion
{
[self showRequestLogWithUrl:path parameter:parameters];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager POST:path
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
NSLog(@"responseObject: %@", responseObject);
completion(YES);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showErrorLog:error];
completion(NO);
}];
}
#pragma mark - 上传下载 -
- (AFHTTPRequestOperation *)uploadImage:(NSString *)uploadPath
imagePath:(NSString *)imagePath
parameter:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURL *filePath = [NSURL fileURLWithPath:imagePath];
return [manager POST:uploadPath parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
success(operation, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
failure(operation, error);
}];
}
- (AFHTTPRequestOperation *)uploadImage:(NSString *)url
path:(NSString *)path
name:(NSString *)name
paramter:(NSDictionary *)parameter
completion:(void (^)(BOOL success))completion
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:name error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Upload Success: %@", responseObject);
completion(YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload Error: %@", error);
completion(NO);
}];
}
- (AFHTTPRequestOperation *)uploadImage:(NSString *)url
imageData:(NSData *)imageData
imageName:(NSString *)imageName
fileName:(NSString*)fileName
paramter:(NSDictionary *)parameter
success:(void (^)(AFHTTPRequestOperation *operation,id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))failure
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
return [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:imageName
fileName:fileName
mimeType:@"image/*"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self showSuccessLog:responseObject];
NSLog(@"Upload Success: %@", responseObject);
success(operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Upload Error: %@", error);
failure(operation, error);
}];
}
- (AFHTTPRequestOperation *)downloadImage:(NSString*)url
saveToFile:(NSString *)path
completion:(void (^)(bool success))block
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
block(YES);
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
block(NO);
}];
[operation start];
return operation;
}
#pragma mark - 日志 -
- (void)showRequestLogWithUrl:(NSString *)path parameter:(NSDictionary *)parameters
{
NSLog(@"========请求路径==========================");
NSLog(@"Request URL: \n%@ \nParameter:\n%@", path, parameters);
}
- (void)showSuccessLog:(id)responseObject
{
NSLog(@"========✔️✔️✔️✔️========================");
NSLog(@"请求成功!返回结果:\n%@", responseObject);
NSLog(@"=========================================");
}
- (void)showErrorLog:(NSError *)error
{
NSLog(@"========❌❌❌❌========================");
NSLog(@"请求失败!错误描述:\n%@", error.localizedDescription);
NSLog(@"=========================================");
}
@end
httpHelper请求辅助类的更多相关文章
- .net Core 使用IHttpClientFactory请求
导读:本文已添加在晨曦微服务之旅,现在自己在尝试微服务架构,一边学边做项目快速的进入状态.当然在学习的过程中会将自己学到的知识进行分享. 一.为什么不用HttpClient ...
- Http请求封装基类HttpHelper.cs
HttpHelper请求封装基类,支持get请求和POS请求http接口交互,为后面接口交互做准备. 1.HttpHelper帮助基类 using System; using System.Colle ...
- 微信公众号开发系列-Http请求封装基类
HttpHelper请求封装基类,支持get请求和POS请求,方便微信开发接口交互,为后面接口交互做准备. 1.HttpHelper帮助基类 [csharp] view plaincopy using ...
- 微信公众号开发系列-Http请求包基类
HttpHelper请求包基类,支持get请求和POS要求.以促进微通道交互界面的开发,为了准备的人机交互界面,背部. 1.HttpHelper帮助基类 [csharp] view plaincopy ...
- C#实现HTTP访问类HttpHelper
在项目开发过程中,我们经常会访问第三方接口,如我们需要接入的第三方接口是Web API,这时候我们就需要使用HttpHelper调用远程接口了.示例中的HttpHelper类使用Log4Net记录了每 ...
- 用java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)
先来看我们的web.xml文件,如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application ...
- .netcore 3.1 C# 微信小程序发送订阅消息
一.appsettings.json定义小程序配置信息 "WX": { "AppId": "wx88822730803edd44", &qu ...
- 使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)
// a[href=#viewSource]"); //查看源代码标签 viewSourceArr.attr("title", "查看源代码"); v ...
- LindDotNetCore~Polly组件对微服务场景的价值
回到目录 Polly是一个开源框架,在github上可以找到,被善友大哥收录,也是.App vNext的一员! App vNext:https://github.com/App-vNext GitHu ...
随机推荐
- 构造函数参数new class[0]的作用
new Class[0];就是传一个长度为1的Class数组过去.内容为null. new Class[0]表示有零个元素的Class数组,即空数组,与传入null结果是一样的,都表示取得无参构造方法 ...
- caffe c++
http://blog.csdn.net/yao_zhuang/article/details/1853625
- SQL一次性插入大量数据【转载】
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...
- cnblogs开源合集
http://www.cnblogs.com/davytitan/p/4135151.html
- QDUOJ 来自xjy的签到题(bfs+状压dp)
来自xjy的签到题 Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...
- Android在Win10环境搭建
一.安装JDK并配置环境变量 1.在Java官方网站上下载相应系统的jdk文件安装,如win7 64 位系统下安装jdk-6u45-windows-x64,全部选择默认的安装路径即可安装完成. 2.配 ...
- 60行代码实现一个迷你版Vue Router
这是一个超级精简版的VueRouter,实现hash模式下,hash改变组件切换的功能,原理就是利用了 Vue.js 的响应式机制触发router-view组件的重新渲染. 代码 https://gi ...
- 代码修改shader Properties uniform变量
2.4 Shader的数据接口:属性和 uniform变量 如果我们想设定一个特定的值到Shader,即为了在实例化Shader为Material时,可以通过为属性赋值达到创建具体对象的目的, ...
- 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery
P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...
- VRTK3.3.0-002获取手柄事件
1.首先创建VRScripts空物体,用来存放脚本,在其下创建Right空物体并添加VRTK_ControllerEvents脚本 2.Right作为右手手柄,拖拽到[VRTK_SDKManager] ...