ASIHTTPRequest 系统需求:git
ASIHTTPRequest Version | Minimum iOS Target | Target Notes
---------------------------|--------------------------|---------------------
1.8.1 -> 1.8.2 | iOS 3.0+ |
0.2 -> 1.8.0 | |程序员
ASIHTTPRequest 使用 MRCgithub
Objective-Cweb
// 添加系统库文件 CFNetwork.framework SystemConfiguration.framework MobileCoreServices.framework CoreGraphics.framework libz.1.1.3.tbd libxml2.2.tbd // 添加第三方库文件 ASIHTTPRequest-1.8.2 // 在 TARGETS -> Builed Settings -> Search Paths -> Header Search Paths 中添加文件路径 /usr/include/libxml2 // 在 TARGETS -> Build Phases -> Compile Sources -> ...in .../ASIHTTPRequest 后添加 -fno-objc-arc // 包含头文件 #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h"
Objective-Capi
// 设置请求头 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"]]; [request addRequestHeader:@"Referer" value:@"http://www.dreamingwish.com/"]; // 设置应用后台运行时是否仍然请求数据 request.shouldContinueWhenAppEntersBackground = YES; // 设置请求超时时重试的次数 request.numberOfTimesToRetryOnTimeout = 3; // 设置 KeepAlive 支持 // Set the amount of time to hang on to a persistent connection before it should expire to 2 minutes request.persistentConnectionTimeoutSeconds = 120; // Disable persistent connections entirely request.shouldAttemptPersistentConnection = NO; // 设置是否显示网络请求信息在 status bar 上 [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO]; // 网络状态检查 BOOL isNetworkInUse = [ASIHTTPRequest isNetworkInUse];
这是 ASIHTTPRequest 最简单的一种使用模式,发送 startSynchronous 消息后即开始在同一线程中执行 HTTP 请求,线程将一直等待直到请求结束(请求成功或者失败)。经过检查 error 属性能够判断请求是否成功或者有错误发生。缓存
要获取返回的文本信息,调用 responseString 方法。若是下载的是二进制文件,例如图片、MP3,则调用 responseData 方法,能够获得一个 NSData 对象。服务器
通常状况下,应该优先使用异步请求代替同步请求,当在主线程中使用 ASIHTTPRequest 同步请求会阻塞主线程的执行,这致使用户界面不响应用户操做,任何动画都会中止渲染,直到请求完成。网络
Objective-Capp
数据请求框架
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"]; // 建立请求 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 设置超时时间,可不设置,使用默认 request.timeOutSeconds = 5; // 发送同步请求 [request startSynchronous]; // 得到错误信息 NSError *error = [request error]; // 网络请求失败 if (error) { // 网络请求成功 NSLog(@"网络请求失败:\n%@", error); } else { // 得到服务器的响应,字符串格式 NSString *responseString = [request responseString]; NSLog(@"网络请求成功:\n%@", responseString); // 得到服务器的响应,NSData 格式 NSData *responseData = [request responseData]; textView.text = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; }
文件下载
若是你想获取下载中的全部数据,能够实现 delegate 中的 request:didReceiveData:方法。但若是你实现了这个方法,request 在下载完后,request 并不把文件放在 downloadDestinationPath中,须要手工处理。
NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/wp-content/uploads/2011/10/asihttprequest-auth.png"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 设置文件存储路径 [request setDownloadDestinationPath:@"/Users/JHQ0228/Desktop/asi.png"]; [request startSynchronous]; // 得到错误信息 NSError *error = [request error]; // 网络请求失败 if (error) { NSLog(@"网络请求失败:\n%@", error); } else { // 网络请求成功 NSLog(@"网络请求成功:\n"); }
请求在后台线程中运行,当请求执行完后再通知调用的线程。这样不会致使主线程进行网络请求时,界面被锁定等状况。
协议方式
在这里实现了两个 delegate 的方法,当数据请求成功时会调用 requestFinished,请求失败时(如网络问题或服务器内部错误)会调用 requestFailed。
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"]; // 建立请求 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 设置超时时间,可不设置,使用默认 request.timeOutSeconds = 5; // 设置代理,需遵照 <ASIHTTPRequestDelegate> 协议 request.delegate = self; // 发送异步请求 [request startAsynchronous]; // 网络请求成功,协议方法 - (void)requestFinished:(ASIHTTPRequest *)request { } // 网络请求失败,协议方法 - (void)requestFailed:(ASIHTTPRequest *)request { }
Block 方式
在平台支持状况下,ASIHTTPRequest 1.8 以上支持 block。
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"]; // 建立请求,加 __weak 除去 block 循环调用警告 __weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 设置超时时间,可不设置,使用默认 request.timeOutSeconds = 5; // 发送异步请求 [request startAsynchronous]; // 网络请求成功 [request setCompletionBlock:^{ }]; // 网络请求失败 [request setFailedBlock:^{ }];
POST 表单
ASIFormDataRequest,模拟 Form 表单提交,其提交格式与 Header 会自动识别。文件中的数据是须要时才从磁盘加载,因此只要 web server 能处理,那么上传大文件是没有问题的。
// 一般数据是以 ’application/x-www-form-urlencoded’ 格式发送的,若是上传了二进制数据或者文件,那么格式将自动变为 ‘multipart/form-data’。 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]]; // 没有文件 [request setPostValue:@"Ben" forKey:@"first_name"]; [request setPostValue:@"Copsey" forKey:@"last_name"]; // 发送文件 [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; // 数据的 mime 头是自动断定的,可是若是你想自定义mime头,那么这样: ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]]; // Upload a file on disk [request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; // Upload an NSData instance NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"myphoto.jpg"]); [request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"]; // 你可使用 addPostValue 方法来发送相同 name 的多个数据: ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]]; [request addPostValue:@"Ben" forKey:@"names"]; [request addPostValue:@"George" forKey:@"names"];
PUT 请求、自定义 POST 请求
若是你想发送 PUT 请求,或者你想自定义 POST 请求,使用 appendPostData: 或者 appendPostDataFromFile:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]]; [request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]]; // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody: [request setRequestMethod:@"PUT"];