[转载]ASIHTTPRequest使用介绍

 ASIHTTPRequest,是一个直接在CFNetwork上做的开源项目,提供了一个比官方更方便更强大的HTTP网络传输的封装。
ASIHTTPRequest 官方网站地址:http://allseeing-i.com/  
一、介绍
特色功能如下:
1.下载的数据直接保存到内存文件系统里
2.提供直接提交(HTTP POST)文件的API
3.可以直接访问与修改HTTP请求与响应HEADER
4.轻松获取上传与下载的进度信息
5.异步请求与队列,自动管理上传与下载队列管理机
6.认证与授权的支持
7.Cookie
8.请求与响应的GZIP
9.代理请求
ASIHTTPRequest -Main classes介绍:
1.ASIHTTPRequest:处理与服务器的基本交互,包括下载上传,认证,cookies以及进度查看。
2.ASIFormDataRequest:是ASIHTTPRequest子类,主要处理post事件,它能使post更加简单。
3.ASINetworkQueue:是NSOperationQueue子类,当处理多个请求时可以使用,如果每次都是单个请求就不必使用。
4.ASIDownloadCache:该类允许ASIHTTPRequest从服务器传递cookie。
ASIHTTPRequest -Support classes介绍:
1.ASIInputStream:当使用ASIHTTPRequest上传数据时使用,如果工程中用了ASIHTTPRequest,就一定要include这个类。
2.ASIAuthenticationDialog:该类允许ASIHTTPRequest连接到服务器时呈现登录框。在所有iPhone OS工程中都要使用,Mac OS工程中可以不用。
3.Reachability:相信很多人对这个类已经很熟悉了,当在你程序中侦测网络状态时它将非常有用。
ASIHTTPRequest -Protocols and configuration介绍:
1.ASIHTTPRequestDelegate:该协议指定了ASIHTTPRequest的delegate可能需要实现的方法,所有方法都是optional。
2.ASIProgressDelegate:该协议列出了uploadProgressDelegate和downloadProgressDelegate可能需要实现的方法,所有方法为optional。
3.ASICacheDelegate:该协议指定了download cache必须实现的方法。如果你要写你自己的download cache,确保实现required方法。
4.ASIHTTPRequestConfig.h: 该文件定义了编译时所有的全局配置选项。使用该文件中的方法可以在控制台中输出request正在进行的任务,Don't forget to turn these off in shipping applicati*****!(这句啥意思?...?时候要关闭?)
使用实例:

- (IBAction)fetchThreeImages:(id)sender

{    

    //清空三个imageview

    [imageView1 setImage:nil];

    [imageView2 setImage:nil];

    [imageView3 setImage:nil];

    

    //初始化一个网络连接对象

    if (!networkQueue) {

        networkQueue = [[ASINetworkQueue allocinit];    

    }

    failed = NO;

    [networkQueue reset];//设网络连接对象,如果代理灯一些设置

    [networkQueue setDownloadProgressDelegate: test];//设置下载进度条的代理

    [networkQueue setRequestDidFinishSelector: @selector(imageFetchComplete:)];//设置下载完成后,所调用的方法

    [networkQueue setRequestDidFailSelector: @selector(imageFetchFailed:)];//设置下载失败调用的方法

    [networkQueue setShowAccurateProgress: YES];//是否显示详细的进度,就是是否有一个连续的进入显示

    [networkQueue setDelegate:self];//设置网络连接对象的代理

    

    ASIHTTPRequest *request;

    //设置下载的地址

    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];

    //设置下载的文件的保持路径

    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];

    //设置用于下载显示的进入的进度条

    [request setDownloadProgressDelegate: imageProgressIndicator1];

    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];

    //添加这个下载

    [networkQueue addOperation:request];

    

    //同上

    request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];

    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];

    [request setDownloadProgressDelegate:imageProgressIndicator2];

    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]];

    [networkQueue addOperation:request];

    

    //同上

    request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];

    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]];

    [request setDownloadProgressDelegate:imageProgressIndicator3];

    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request3" forKey:@"name"]];

    [networkQueue addOperation:request];

    

    //开始下

    [networkQueue go];

}

posted @ 2011-10-09 23:22  jacktu  阅读(532)  评论(0编辑  收藏  举报