ASI基于CFNetwork框架开发,而AFN基于NSURL.

ASI更底层,请求使用创建CFHTTPMessageRef进行,使用NSOperationQueue进行管理,ASIHTTPRequest就是NSOpration的子类,并实现了NSCopy协议。使用static NSOperationQueue *sharedQueue, 在ASIHTTPRequest执行网络请求时把自己加进去queue。

AFN基于NSURL,请求使用NSURLRequest作为参数传入NSURlconnection进行。使用NSOperationQueue进行管理,通过初始化AFHTTPRquestOperationManager进行多线程管理。

更多可以参考 对比IOS网络组件

2.优缺点对比

选自: AFNetworking、MKNetworkKit和ASIHTTPRequest对比

ASI开发者已于2012年10月宣布暂停该开源库的更新.

AFN的活跃维护者比较多。

3.使用

1.ASI的大概实现

ASIHTTPRequest是NSOperation的子类。 在ASIHTTPRequest有个初始方法:

  1. - (id)initWithURL:(NSURL *)newURL
  2. {
  3. self = [self init];
  4. [self setRequestMethod:@"GET"];
  5.  
  6. [self setRunLoopMode:NSDefaultRunLoopMode];
  7. [self setShouldAttemptPersistentConnection:YES];
  8. [self setPersistentConnectionTimeoutSeconds:60.0];
  9. [self setShouldPresentCredentialsBeforeChallenge:YES];
  10. [self setShouldRedirect:YES];
  11. [self setShowAccurateProgress:YES];
  12. [self setShouldResetDownloadProgress:YES];
  13. [self setShouldResetUploadProgress:YES];
  14. [self setAllowCompressedResponse:YES];
  15. [self setShouldWaitToInflateCompressedResponses:YES];
  16. [self setDefaultResponseEncoding:NSISOLatin1StringEncoding];
  17. [self setShouldPresentProxyAuthenticationDialog:YES];
  18.  
  19. [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]];
  20. [self setUseSessionPersistence:YES];
  21. [self setUseCookiePersistence:YES];
  22. [self setValidatesSecureCertificate:YES];
  23. [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
  24. [self setDidStartSelector:@selector(requestStarted:)];
  25. [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)];
  26. [self setWillRedirectSelector:@selector(request:willRedirectToURL:)];
  27. [self setDidFinishSelector:@selector(requestFinished:)];
  28. [self setDidFailSelector:@selector(requestFailed:)];
  29. [self setDidReceiveDataSelector:@selector(request:didReceiveData:)];
  30. [self setURL:newURL];
  31. [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]];
  32. [self setDownloadCache:[[self class] defaultCache]];
  33. return self;
  34. }

然后在执行异步网络访问时,把自己扔进shareQueue进行管理。

  1. - (void)startAsynchronous
  2. {
  3. #if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING
  4. ASI_DEBUG_LOG(@"[STATUS] Starting asynchronous request %@",self);
  5. #endif
  6. [sharedQueue addOperation:self];
  7. }

执行同步访问时更直接。注意[self main]. main方法里面执行了CFNetwork的操作。

  1. - (void)startSynchronous
  2. {
  3. #if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING
  4. ASI_DEBUG_LOG(@"[STATUS] Starting synchronous request %@",self);
  5. #endif
  6. [self setSynchronous:YES];
  7. [self setRunLoopMode:ASIHTTPRequestRunLoopMode];
  8. [self setInProgress:YES];
  9.  
  10. if (![self isCancelled] && ![self complete]) {
  11. [self main];
  12. while (!complete) {
  13. [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]];
  14. }
  15. }
  16.  
  17. [self setInProgress:NO];
  18. }

2.ASI基本使用

  1. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"XXXXX"]];
  2. [request setDelegate:self]; //ASIHTTPRequestDelegate
  3. [request startAsynchronous];

不用进行很多配置,是因为ASIHTTPRequest的requestWithURL方法有默认配置,可以在实例化后自己再修改。如下(还有更多的自己阅读源码):

  1. [self setRequestMethod:@"GET"]; //访问方法
  2.  
  3. [self setRunLoopMode:NSDefaultRunLoopMode]; //默认runloop
  4. [self setShouldAttemptPersistentConnection:YES]; //设置持久连接,重用request时用节约
  5. [self setPersistentConnectionTimeoutSeconds:60.0];
  6. [self setShouldPresentCredentialsBeforeChallenge:YES]; //是否要证书验证
  7. [self setShouldRedirect:YES];
  8. [self setShowAccurateProgress:YES]; //进度
  9. [self setShouldResetDownloadProgress:YES];
  10. [self setShouldResetUploadProgress:YES];
  11. [self setAllowCompressedResponse:YES];
  12. [self setShouldWaitToInflateCompressedResponses:YES];
  13. [self setDefaultResponseEncoding:NSISOLatin1StringEncoding];
  14. [self setShouldPresentProxyAuthenticationDialog:YES];
  15.  
  16. [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]]; //请求的网络等待时长
  17. [self setUseSessionPersistence:YES]; //保持session
  18. [self setUseCookiePersistence:YES]; //保持cookie
  19. [self setValidatesSecureCertificate:YES];
  20. [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]];
  21. [self setDidStartSelector:@selector(requestStarted:)]; //请求开始
  22. [self setDidReceiveResponseHeadersSelector:@selector(request:didReceiveResponseHeaders:)]; //获取到ResponseHeader
  23. [self setWillRedirectSelector:@selector(request:willRedirectToURL:)];
  24. [self setDidFinishSelector:@selector(requestFinished:)]; //请求完成
  25. [self setDidFailSelector:@selector(requestFailed:)]; //请求失败
  26. [self setDidReceiveDataSelector:@selector(request:didReceiveData:)]; //获取到data,多次
  27. [self setURL:newURL]; //设置URL
  28. [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]];
  29. [self setDownloadCache:[[self class] defaultCache]];

ASIHTTPRequestDelegate的代理:

  1. - (void)requestStarted:(ASIHTTPRequest *)request;
  2. - (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
  3. - (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
  4. - (void)requestFinished:(ASIHTTPRequest *)request;
  5. - (void)requestFailed:(ASIHTTPRequest *)request;
  6. - (void)requestRedirected:(ASIHTTPRequest *)request;
  7.  
  8. - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data;
  9.  
  10. - (void)authenticationNeededForRequest:(ASIHTTPRequest *)request;
  11. - (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request;

2.AFN基本使用

1.实现基本原理:

先看看AFHTTPRquestOperationManager的默认初始化方法:可以看出默认的request为二进制,reponse为json解析。可以根据业务进行修改。

  1. - (instancetype)initWithBaseURL:(NSURL *)url {
  2. self = [super init];
  3. if (!self) {
  4. return nil;
  5. }
  6.  
  7. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  8. if ([[url path] length] > && ![[url absoluteString] hasSuffix:@"/"]) {
  9. url = [url URLByAppendingPathComponent:@""];
  10. }
  11.  
  12. self.baseURL = url; //初始化了baseurl,比如你的访问地址是http://192.168.0.100/login.action . 初始化baseUrl为http://192.168.0.100/ , 以后manager GET:@"login.action"即可。
  13.  
  14. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  15. self.responseSerializer = [AFJSONResponseSerializer serializer];
  16.  
  17. self.securityPolicy = [AFSecurityPolicy defaultPolicy]; //AFSSLPinningModeNone无隐私要求
  18.  
  19. self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
  20.  
  21. self.operationQueue = [[NSOperationQueue alloc] init];
  22.  
  23. self.shouldUseCredentialStorage = YES;
  24.  
  25. return self;
  26. }

再看看其中一个方法GET方法。

AFHTTPRequestOperation 是 AFURLConnectionOperation的子类,AFURLConnectionOperation的子类是NSOperation的子类,并实现了NSURLConnectionDelegate等网络协议。

@interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSSecureCoding, NSCopying>

是使用刚才manager初始化生成的operationQueue进行多线程管理,所以一个项目有一个manager然后用来管理网络请求就行了。多线程已经由AFN内部处理了。

  1. - (AFHTTPRequestOperation *)GET:(NSString *)URLString
  2. parameters:(id)parameters
  3. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  4. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  5. {
  6. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure];
  7.  
  8. [self.operationQueue addOperation:operation];
  9. return operation;
  10. }

AFHTTPRequestOperation的生成,复用了很多manager的属性:

  1. - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
  2. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  3. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  4. {
  5. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  6. operation.responseSerializer = self.responseSerializer;
  7. operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
  8. operation.credential = self.credential;
  9. operation.securityPolicy = self.securityPolicy;
  10.  
  11. [operation setCompletionBlockWithSuccess:success failure:failure]; //注意这个地方
  12. operation.completionQueue = self.completionQueue;
  13. operation.completionGroup = self.completionGroup;
  14.  
  15. return operation;
  16. }

最后的回调,AFN是使用了NSOperation自己的block,难怪在协议找了好久没找到

  1. - (void)setCompletionBlock:(void (^)(void))block {
  2. [self.lock lock];
  3. if (!block) {
  4. [super setCompletionBlock:nil];
  5. } else {
  6. __weak __typeof(self)weakSelf = self;
  7. [super setCompletionBlock:^ {
  8. __strong __typeof(weakSelf)strongSelf = weakSelf;
  9.  
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Wgnu"
  12. dispatch_group_t group = strongSelf.completionGroup ?: url_request_operation_completion_group();
  13. dispatch_queue_t queue = strongSelf.completionQueue ?: dispatch_get_main_queue();
  14. #pragma clang diagnostic pop
  15.  
  16. dispatch_group_async(group, queue, ^{
  17. block();
  18. });
  19.  
  20. dispatch_group_notify(group, url_request_operation_completion_queue(), ^{
  21. [strongSelf setCompletionBlock:nil];
  22. });
  23. }];
  24. }
  25. [self.lock unlock];
  26. }

2.基本GET

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  2. [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
  3. NSLog(@"JSON: %@", responseObject);
  4. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  5. NSLog(@"Error: %@", error);
  6. }];

3.下载一个文件

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  3.  
  4. NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
  5. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  6.  
  7. NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  8. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  9. return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  10. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  11. NSLog(@"File downloaded to: %@", filePath);
  12. }];
  13. [downloadTask resume];
 

IOS网络请求框架AFNetworking和ASIHttpRequest对比的更多相关文章

  1. 对比iOS网络组件:AFNetworking VS ASIHTTPRequest

    对比iOS网络组件:AFNetworking VS ASIHTTPRequest 作者 高嘉峻 发布于 2013年2月28日 | 7 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...

  2. 对比iOS网络组件:AFNetworking VS ASIHTTPRequest(转载)

    在开发iOS应用过程中,如何高效的与服务端API进行数据交换,是一个常见问题.一般开发者都会选择一个第三方的网络组件作为服务,以提高开发效率和稳定性.这些组件把复杂的网络底层操作封装成友好的类和方法, ...

  3. IOS网络请求之AFNetWorking 3.x 使用

    前言: 计划把公司的网络请求与业务解耦,所以想着学习一下网络请求,最近学习了NSURLSession,今天来学习一下基于NSURLSession封装的优秀开源框架AFNetWorking 3.x,之前 ...

  4. 安卓开发常用网络请求框架OkHttp、Volley、XUtils、Retrofit对比

    网络请求框架总结1.xutils     此框架庞大而周全,这个框架可以网络请求,同时可以图片加载,又可以数据存储,又可以 View 注解,使用这种框架很方便,这样会使得你整个项目对它依赖性太强,万一 ...

  5. IOS网络请求之NSURLSession使用

    前言: 无论是Android还是ios都离不开与服务器交互,这就必须用到网络请求,记得在2013年做iOS的时候那时候用的ASIHTTPRequest框架,现在重新捡起iOS的时候ASIHTTPReq ...

  6. 【转载】一步一步搭建自己的iOS网络请求库

    一步一步搭建自己的iOS网络请求库(一) 大家好,我是LastDay,很久没有写博客了,这周会分享一个的HTTP请求库的编写经验. 简单的介绍 介绍一下,NSURLSession是iOS7中新的网络接 ...

  7. android翻译应用、地图轨迹、视频广告、React Native知乎日报、网络请求框架等源码

    Android精选源码 android实现高德地图轨迹效果源码 使用React Native(Android和iOS)实现的 知乎日报效果源码 一款整合百度翻译api跟有道翻译api的翻译君 RxEa ...

  8. 2020,最新APP重构:网络请求框架

    在现在的app,网络请求是一个很重要的部分,app中很多部分都有或多或少的网络请求,所以在一个项目重构时,我会选择网络请求框架作为我重构的起点.在这篇文章中我所提出的架构,并不是所谓的 最好 的网络请 ...

  9. Android网络请求框架

    本篇主要介绍一下Android中经常用到的网络请求框架: 客户端网络请求,就是客户端发起网络请求,经过网络框架的特殊处理,让后将请求发送的服务器,服务器根据 请求的参数,返回客户端需要的数据,经过网络 ...

随机推荐

  1. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  2. Unity3D-美术相关

    1.导入动画 (1)使用预分解的动画模型,导入后面板中会含有一个可用的动画片段列表. (2)使用未分解的动画模型,自行添加动画片段. (3)使用多个动画文件,模型与动画分离.对于goober.fbx模 ...

  3. asp.net mvc 实体类成员变量标识示例

    检查不能为空 [Required] public string ID { get; set; } 检查最大长度 [StringLength(36, ErrorMessage = "长度不可超 ...

  4. memcache和memcahced区别

    在写这篇文章之前一直对memcache .memcahced模糊,相差一个字母,特此总结下: Memcache是什么? Memcache是一个自由和开放源代码.高性能.分配的内存对象缓存系统.用于加速 ...

  5. Linux下c/c++项目代码覆盖率的产生方法

    最近做了一系列的单元测试相关的工作,除了各种规范及测试框架以外,讨论比较多的就是关于代码覆盖率的产生,c/c++与其他的一些高级语言或者脚本语言相比较而言,例如 Java..Net和php/pytho ...

  6. oracle 全文检索技术

    1.查看用户: select * from dba_users WHERE username='CTXSYS';select * from dba_users WHERE username='CTXS ...

  7. ios开源项目(各种有用的第三方库)

    状态栏:MTStatusBarOverlay  下拉刷新:EGOTableViewPullRefresh  网络应用:ASIHTTPRequest  等待特效:MBProgressHUD  JSON解 ...

  8. MongoDB 快速入门--高级

    引用 --------->DBRefs DBRef的形式: { $ref : , $id : , $db : } $ref:集合名称 $id:引用的id $db:数据库名称,可选参数 { &qu ...

  9. Rstudio设置永久工作路径

    Rstudio中 getwd() 获取工作路径 setwd() 设置工作路径 但这种方式设置后每次打开都要重新设置,现在介绍一种设置永久路径的方法

  10. oracle中extents存在的理由

    extents的特性:1:一个extent由相连的多个blocks组成,多个extents组成一个segment,2:oracle在为segment分配空间时,是以extent为单位因此extents ...