iPhone应用中HTTP包装开源项目ASIHTTPRequest详解

移动开发 iOS
ASIHTTPRequest是简单易用的,它封装了CFNetwork API。使得与Web服务器通信变得更简单。它是用Objective-C编写的,可以在MAC OS X和iPhone应用中使用。

ASIHTTPRequest一个强大的HTTP包装开源项目是本文要介绍的内容,ASIHTTPRequest是什么?ASIHTTPRequest是简单易用的,它封装了CFNetwork API。使得与Web服务器通信变得更简单。它是用Objective-C编写的,可以在MAC OS X和iPhone应用中使用。

它适用于执行基本的HTTP请求和互动(或者说是反馈)。ASIFormDataRequest子类可以简单的实现提交数据和文件。使用multipart/form-data

提供了以下:

一个从web服务器提交和获取数据的接口

直接下载数据到内存或者本地文件系统里

能够从本地提交文件,作为post数据的一部分。兼容HTML file input mechanism

可以访问和修改http请求和响应header

获得上传下载的进度信息

异步请求和队列,自动管理上传下载队列机制

cookie 支持

请求和响应的gzip支持

代理请求

ASIHTTPRequest设置

在iphone 项目中使用ASIHTTPRequest

1、添加一些必要的文件,复制以下文件到项目中去

  1. ASIHTTPRquestConfig.h  
  2. ASInputStream.h  
  3. ASInputStream.m  
  4. ASIHTTPRequest.h  
  5. ASIHTTPRequest.h  
  6. ASINSStringAdditions.h  
  7. ASINSStringAdditions.m  
  8. ASIFormDataRequest.h  
  9. ASIFormDataRequest.m  
  10. ASINetworkQueue.h  
  11. ASINetworkQueue.m 

iphone项目还必须包含以下文件

  1. ASIAuthenticationDialog.h  
  2. ASIAuthenticationDialog.m 

一个版本的Reachability类

添加必要的框架到项目中去

  1. CFNetwork.framework  
  2. SystemConfiguration.framework  
  3. libz.1.2.3.dylib 

配置Reachability

在iphone上,ASIHTTPRequest使用Apple的Reachability类。

Reachability有两个版本,他们都能在ASIHTTPRequest发行文件的Reachability文件夹中找到。

2.0版本是最新的办迸。如果你的项目是基于iphone os 3.x和更新的系统,你应该使用2.0版本的。包括.h和.m文件。保证在ASIHTTPRequestConfig.h文件中REACHABILITY_20_API的值为1

1.5是个老版本,它和iphone os 2.2.1-iphone os 3.0兼容。保证在ASIHTTPRequestConfig.h文件中REACHABILITY_20_API的值为0

在mac ox x项目中使用AHIHTTPRequest

为了在Mac os x项目中使用ASIHTTPRequest,你需要导入以下:

  1. SystemConfiguration.framework + zlib  
  2. CoreService.framework 

在Mac OS X上,CFNetwork 是CoreServices框架的一部分。除非你写的是基于控制台的应用程序,官方地址:http://allseeing-i.com/ASIHTTPRequest/

ASIHTTPRequest,是一个直接在CFNetwork上做的开源项目,提供了一个比官方更方便更强大的HTTP网络传输的封装。

特色功能如下:

1、下载的数据直接保存到内存或文件系统里

2、提供直接提交(HTTP POST)文件的API

3、可以直接访问与修改HTTP请求与响应HEADER

4、轻松获取上传与下载的进度信息

5、异步请求与队列,自动管理上传与下载队列管理机

6、认证与授权的支持

7、Cookie

8、请求与响应的GZIP

9、代理请求

下面来两个小例子:

  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  2. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  3. [request start];  
  4. NSError *error = [request error];  
  5. if (!error) {  
  6. NSString *response = [request responseString];  

当你需要添加更多的请求信息时,如,添加个请求Header:

  1. [request addRequestHeader:@"name" value:@"Jory lee"]; 

添加Post请求时的健值:

  1. [request setPostValue:@"Ben" forKey:@"first_name"];  
  2. [request setPostValue:@"Copsey" forKey:@"last_name"];  
  3. [request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"]; 

设置HTTP的授权帐号:

  1. [request setUsername:@"username"];  
  2. [request setPassword:@"password"]; 

一个异步请求:

  1. - (IBAction)grabURLInBackground:(id)sender  
  2. {  
  3. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5. [request setDelegate:self];  
  6. [request startAsynchronous];  
  7. }  
  8. - (void)requestFinished:(ASIHTTPRequest *)request  
  9. {  
  10. // Use when fetching text data  
  11. NSString *responseString = [request responseString];  
  12. // Use when fetching binary data  
  13. NSData *responseData = [request responseData];  
  14. }  
  15. - (void)requestFailed:(ASIHTTPRequest *)request  
  16. {  
  17. NSError *error = [request error];  
  18. }    

在我们数据获取的过程中,如果数据源复杂,一个请求队列是必不可少的:

  1. - (IBAction)grabURLInTheBackground:(id)sender  
  2. {  
  3. if (![self queue]) {  
  4. [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];  
  5. }  
  6. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  7. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  8. [request setDelegate:self];  
  9. [request setDidFinishSelector:@selector(requestDone:)];  
  10. [request setDidFailSelector:@selector(requestWentWrong:)];  
  11. [[self queue] addOperation:request]; //queue is an NSOperationQueue  
  12. }  
  13. - (void)requestDone:(ASIHTTPRequest *)request  
  14. {  
  15. NSString *response = [request responseString];  
  16. }  
  17. - (void)requestWentWrong:(ASIHTTPRequest *)request  
  18. {  
  19. NSError *error = [request error];  
  20. }    

小结:iPhone应用中HTTP包装开源项目ASIHTTPRequest详解的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-09 14:08:51

iPhoneHTTP请求协议

2011-08-22 10:06:38

IOS开发ASIHTTPRequHTTP 请求

2011-08-12 13:35:23

iPhone文件流ASIHTTPRequ

2011-08-02 17:27:06

iPhone应用 剪切技巧

2011-08-12 14:33:06

iPhone缓存文件

2011-08-15 11:37:20

iPhone开发Mask

2011-08-17 10:16:35

iPhone应用HTTP请求协议

2011-08-03 17:18:58

iPhone UILabel UISlider

2011-08-17 15:10:21

iPhone开发Web视图

2014-06-05 10:21:29

HTTP

2011-08-19 14:14:14

iPhone应用

2011-07-27 11:14:37

iPhone UITableVie

2011-07-08 17:45:19

iPhone 文档

2011-08-12 10:04:24

iPhone开发视图

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-08-17 15:19:38

iPhone应用数据

2011-07-19 14:36:32

iPhone

2011-08-02 17:14:41

iPhone应用 UITableVie

2009-03-05 18:20:58

开源项目IphoneAndroid
点赞
收藏

51CTO技术栈公众号