IOS网络请求框架AFNetworking和ASIHttpRequest对比
ASI基于CFNetwork框架开发,而AFN基于NSURL.
ASI更底层,请求使用创建CFHTTPMessageRef进行,使用NSOperationQueue进行管理,ASIHTTPRequest就是NSOpration的子类,并实现了NSCopy协议。使用static NSOperationQueue *sharedQueue, 在ASIHTTPRequest执行网络请求时把自己加进去queue。
AFN基于NSURL,请求使用NSURLRequest作为参数传入NSURlconnection进行。使用NSOperationQueue进行管理,通过初始化AFHTTPRquestOperationManager进行多线程管理。
更多可以参考 对比IOS网络组件
2.优缺点对比
3.使用
1.ASI的大概实现
ASIHTTPRequest是NSOperation的子类。 在ASIHTTPRequest有个初始方法:
- - (id)initWithURL:(NSURL *)newURL
- {
- self = [self init];
- [self setRequestMethod:@"GET"];
- [self setRunLoopMode:NSDefaultRunLoopMode];
- [self setShouldAttemptPersistentConnection:YES];
- [self setPersistentConnectionTimeoutSeconds:60.0];
- [self setShouldPresentCredentialsBeforeChallenge:YES];
- [self setShouldRedirect:YES];
- [self setShowAccurateProgress:YES];
- [self setShouldResetDownloadProgress:YES];
- [self setShouldResetUploadProgress:YES];
- [self setAllowCompressedResponse:YES];
- [self setShouldWaitToInflateCompressedResponses:YES];
- [self setDefaultResponseEncoding:NSISOLatin1StringEncoding];
- [self setShouldPresentProxyAuthenticationDialog:YES];
- [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]];
- [self setUseSessionPersistence:YES];
- [self setUseCookiePersistence:YES];
- [self setValidatesSecureCertificate:YES];
- [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
- [self setDidStartSelector:@selector(requestStarted:)];
- [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)];
- [self setWillRedirectSelector:@selector(request:willRedirectToURL:)];
- [self setDidFinishSelector:@selector(requestFinished:)];
- [self setDidFailSelector:@selector(requestFailed:)];
- [self setDidReceiveDataSelector:@selector(request:didReceiveData:)];
- [self setURL:newURL];
- [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]];
- [self setDownloadCache:[[self class] defaultCache]];
- return self;
- }
然后在执行异步网络访问时,把自己扔进shareQueue进行管理。
- - (void)startAsynchronous
- {
- #if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING
- ASI_DEBUG_LOG(@"[STATUS] Starting asynchronous request %@",self);
- #endif
- [sharedQueue addOperation:self];
- }
执行同步访问时更直接。注意[self main]. main方法里面执行了CFNetwork的操作。
- - (void)startSynchronous
- {
- #if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING
- ASI_DEBUG_LOG(@"[STATUS] Starting synchronous request %@",self);
- #endif
- [self setSynchronous:YES];
- [self setRunLoopMode:ASIHTTPRequestRunLoopMode];
- [self setInProgress:YES];
- if (![self isCancelled] && ![self complete]) {
- [self main];
- while (!complete) {
- [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]];
- }
- }
- [self setInProgress:NO];
- }
2.ASI基本使用
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"XXXXX"]];
- [request setDelegate:self]; //ASIHTTPRequestDelegate
- [request startAsynchronous];
不用进行很多配置,是因为ASIHTTPRequest的requestWithURL方法有默认配置,可以在实例化后自己再修改。如下(还有更多的自己阅读源码):
- [self setRequestMethod:@"GET"]; //访问方法
- [self setRunLoopMode:NSDefaultRunLoopMode]; //默认runloop
- [self setShouldAttemptPersistentConnection:YES]; //设置持久连接,重用request时用节约
- [self setPersistentConnectionTimeoutSeconds:60.0];
- [self setShouldPresentCredentialsBeforeChallenge:YES]; //是否要证书验证
- [self setShouldRedirect:YES];
- [self setShowAccurateProgress:YES]; //进度
- [self setShouldResetDownloadProgress:YES];
- [self setShouldResetUploadProgress:YES];
- [self setAllowCompressedResponse:YES];
- [self setShouldWaitToInflateCompressedResponses:YES];
- [self setDefaultResponseEncoding:NSISOLatin1StringEncoding];
- [self setShouldPresentProxyAuthenticationDialog:YES];
- [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]]; //请求的网络等待时长
- [self setUseSessionPersistence:YES]; //保持session
- [self setUseCookiePersistence:YES]; //保持cookie
- [self setValidatesSecureCertificate:YES];
- [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
- [self setDidStartSelector:@selector(requestStarted:)]; //请求开始
- [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)]; //获取到ResponseHeader
- [self setWillRedirectSelector:@selector(request:willRedirectToURL:)];
- [self setDidFinishSelector:@selector(requestFinished:)]; //请求完成
- [self setDidFailSelector:@selector(requestFailed:)]; //请求失败
- [self setDidReceiveDataSelector:@selector(request:didReceiveData:)]; //获取到data,多次
- [self setURL:newURL]; //设置URL
- [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]];
- [self setDownloadCache:[[self class] defaultCache]];
ASIHTTPRequestDelegate的代理:
- - (void)requestStarted:(ASIHTTPRequest *)request;
- - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
- - (void)requestFinished:(ASIHTTPRequest *)request;
- - (void)requestFailed:(ASIHTTPRequest *)request;
- - (void)requestRedirected:(ASIHTTPRequest *)request;
- - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
- - (void)authenticationNeededForRequest:(ASIHTTPRequest *)request;
- - (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request;
2.AFN基本使用
1.实现基本原理:
先看看AFHTTPRquestOperationManager的默认初始化方法:可以看出默认的request为二进制,reponse为json解析。可以根据业务进行修改。
- - (instancetype)initWithBaseURL:(NSURL *)url {
- self = [super init];
- if (!self) {
- return nil;
- }
- // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
- if ([[url path] length] > && ![[url absoluteString] hasSuffix:@"/"]) {
- url = [url URLByAppendingPathComponent:@""];
- }
- self.baseURL = url; //初始化了baseurl,比如你的访问地址是http://192.168.0.100/login.action . 初始化baseUrl为http://192.168.0.100/ , 以后manager GET:@"login.action"即可。
- self.requestSerializer = [AFHTTPRequestSerializer serializer];
- self.responseSerializer = [AFJSONResponseSerializer serializer];
- self.securityPolicy = [AFSecurityPolicy defaultPolicy]; //AFSSLPinningModeNone无隐私要求
- self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
- self.operationQueue = [[NSOperationQueue alloc] init];
- self.shouldUseCredentialStorage = YES;
- return self;
- }
再看看其中一个方法GET方法。
AFHTTPRequestOperation 是 AFURLConnectionOperation的子类,AFURLConnectionOperation的子类是NSOperation的子类,并实现了NSURLConnectionDelegate等网络协议。
@interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSSecureCoding, NSCopying>
是使用刚才manager初始化生成的operationQueue进行多线程管理,所以一个项目有一个manager然后用来管理网络请求就行了。多线程已经由AFN内部处理了。
- - (AFHTTPRequestOperation *)GET:(NSString *)URLString
- parameters:(id)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
- [self.operationQueue addOperation:operation];
- return operation;
- }
AFHTTPRequestOperation的生成,复用了很多manager的属性:
- - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- operation.responseSerializer = self.responseSerializer;
- operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
- operation.credential = self.credential;
- operation.securityPolicy = self.securityPolicy;
- [operation setCompletionBlockWithSuccess:success failure:failure]; //注意这个地方
- operation.completionQueue = self.completionQueue;
- operation.completionGroup = self.completionGroup;
- return operation;
- }
最后的回调,AFN是使用了NSOperation自己的block,难怪在协议找了好久没找到
- - (void)setCompletionBlock:(void (^)(void))block {
- [self.lock lock];
- if (!block) {
- [super setCompletionBlock:nil];
- } else {
- __weak __typeof(self)weakSelf = self;
- [super setCompletionBlock:^ {
- __strong __typeof(weakSelf)strongSelf = weakSelf;
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wgnu"
- dispatch_group_t group = strongSelf.completionGroup ?: url_request_operation_completion_group();
- dispatch_queue_t queue = strongSelf.completionQueue ?: dispatch_get_main_queue();
- #pragma clang diagnostic pop
- dispatch_group_async(group, queue, ^{
- block();
- });
- dispatch_group_notify(group, url_request_operation_completion_queue(), ^{
- [strongSelf setCompletionBlock:nil];
- });
- }];
- }
- [self.lock unlock];
- }
2.基本GET
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"JSON: %@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"Error: %@", error);
- }];
3.下载一个文件
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
- NSURLRequest *request = [NSURLRequest requestWithURL:URL];
- NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
- NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
- return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
- } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- NSLog(@"File downloaded to: %@", filePath);
- }];
- [downloadTask resume];
IOS网络请求框架AFNetworking和ASIHttpRequest对比的更多相关文章
- 对比iOS网络组件:AFNetworking VS ASIHTTPRequest
对比iOS网络组件:AFNetworking VS ASIHTTPRequest 作者 高嘉峻 发布于 2013年2月28日 | 7 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...
- 对比iOS网络组件:AFNetworking VS ASIHTTPRequest(转载)
在开发iOS应用过程中,如何高效的与服务端API进行数据交换,是一个常见问题.一般开发者都会选择一个第三方的网络组件作为服务,以提高开发效率和稳定性.这些组件把复杂的网络底层操作封装成友好的类和方法, ...
- IOS网络请求之AFNetWorking 3.x 使用
前言: 计划把公司的网络请求与业务解耦,所以想着学习一下网络请求,最近学习了NSURLSession,今天来学习一下基于NSURLSession封装的优秀开源框架AFNetWorking 3.x,之前 ...
- 安卓开发常用网络请求框架OkHttp、Volley、XUtils、Retrofit对比
网络请求框架总结1.xutils 此框架庞大而周全,这个框架可以网络请求,同时可以图片加载,又可以数据存储,又可以 View 注解,使用这种框架很方便,这样会使得你整个项目对它依赖性太强,万一 ...
- IOS网络请求之NSURLSession使用
前言: 无论是Android还是ios都离不开与服务器交互,这就必须用到网络请求,记得在2013年做iOS的时候那时候用的ASIHTTPRequest框架,现在重新捡起iOS的时候ASIHTTPReq ...
- 【转载】一步一步搭建自己的iOS网络请求库
一步一步搭建自己的iOS网络请求库(一) 大家好,我是LastDay,很久没有写博客了,这周会分享一个的HTTP请求库的编写经验. 简单的介绍 介绍一下,NSURLSession是iOS7中新的网络接 ...
- android翻译应用、地图轨迹、视频广告、React Native知乎日报、网络请求框架等源码
Android精选源码 android实现高德地图轨迹效果源码 使用React Native(Android和iOS)实现的 知乎日报效果源码 一款整合百度翻译api跟有道翻译api的翻译君 RxEa ...
- 2020,最新APP重构:网络请求框架
在现在的app,网络请求是一个很重要的部分,app中很多部分都有或多或少的网络请求,所以在一个项目重构时,我会选择网络请求框架作为我重构的起点.在这篇文章中我所提出的架构,并不是所谓的 最好 的网络请 ...
- Android网络请求框架
本篇主要介绍一下Android中经常用到的网络请求框架: 客户端网络请求,就是客户端发起网络请求,经过网络框架的特殊处理,让后将请求发送的服务器,服务器根据 请求的参数,返回客户端需要的数据,经过网络 ...
随机推荐
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- Unity3D-美术相关
1.导入动画 (1)使用预分解的动画模型,导入后面板中会含有一个可用的动画片段列表. (2)使用未分解的动画模型,自行添加动画片段. (3)使用多个动画文件,模型与动画分离.对于goober.fbx模 ...
- asp.net mvc 实体类成员变量标识示例
检查不能为空 [Required] public string ID { get; set; } 检查最大长度 [StringLength(36, ErrorMessage = "长度不可超 ...
- memcache和memcahced区别
在写这篇文章之前一直对memcache .memcahced模糊,相差一个字母,特此总结下: Memcache是什么? Memcache是一个自由和开放源代码.高性能.分配的内存对象缓存系统.用于加速 ...
- Linux下c/c++项目代码覆盖率的产生方法
最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...
- oracle 全文检索技术
1.查看用户: select * from dba_users WHERE username='CTXSYS';select * from dba_users WHERE username='CTXS ...
- ios开源项目(各种有用的第三方库)
状态栏:MTStatusBarOverlay 下拉刷新:EGOTableViewPullRefresh 网络应用:ASIHTTPRequest 等待特效:MBProgressHUD JSON解 ...
- MongoDB 快速入门--高级
引用 --------->DBRefs DBRef的形式: { $ref : , $id : , $db : } $ref:集合名称 $id:引用的id $db:数据库名称,可选参数 { &qu ...
- Rstudio设置永久工作路径
Rstudio中 getwd() 获取工作路径 setwd() 设置工作路径 但这种方式设置后每次打开都要重新设置,现在介绍一种设置永久路径的方法
- oracle中extents存在的理由
extents的特性:1:一个extent由相连的多个blocks组成,多个extents组成一个segment,2:oracle在为segment分配空间时,是以extent为单位因此extents ...