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 ...
随机推荐
- 数论 最简分数 Farey序列求最简分数+POJ3374
法雷数列 定义和定理 定义一: 最简分数(也称既约分数或不可约分数).若p,q的最大公约数是1,我们称分数p/q是最简分数. 定义二: 真分数,若p,q是正整数,0<p/q<1, 我们说p ...
- GridView中数据行的操作
一个是直接动态绑定gridview 用3楼的办法就可以了 int j=1;//j的数值表示你要取的那一列的索引,要取第二列,j就设为1for (int i = 0; i < this.GridV ...
- Weblogic学习
1.WebLogic目录介绍: coherence:集群组件 modules:第三方的模块包 user_projects:存放域的文件夹(必须要创建域) utils 工具包 uninstall ...
- hadoop学习路线(转)
刚刚入门hadoop,如何去学习hadoop.google一篇学习路线图,与童鞋们共勉: 转自:http://blog.csdn.net/zhoudaxia/article/details/88017 ...
- Acdream手速赛7
蛋疼啊,本次只做出了一道题目...渣爆了... 妈蛋,,卡题之夜..比赛结果是1道题,比赛完哗啦哗啦出4道题.. A acdream1191 Dragon Maze 题意: 给一个迷宫,给出入口坐标和 ...
- GitBook 入门学习
一.什么是 Gitbook GitBook 是一个基于 Node.js 的命令行工具,支持 Markdown 和 AsciiDoc 两种语法格式,可以输出 HTML.PDF.eBook 等格式的电子书 ...
- unite2017相关
日程 http://unite2017.csdn.net/ http://www.sohu.com/a/137202360_280780 http://www.gameres.com/750046.h ...
- [NOIP2012] 摆花
1270. [NOIP2012] 摆花 http://cogs.pro/cogs/problem/problem.php?pid=1270 ★ 输入文件:flower.in 输出文件:flow ...
- [Xcode 实际操作]七、文件与数据-(5 )复制、移动、删除文件和删除文件夹
目录:[Swift]Xcode实际操作 本文将演示如何复制.移动和删除文件. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ...
- thinkphp5实现mysql数据库还原
数据库还原其实就是从.sql文件中读取一行一行的命令,然后执行 需要配置数据库文件database.php,数据库名,主机名,用户名,密码这里就不说了,这里说的要配置数据库连接参数 'params' ...