·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> ASIHTTPRequest

ASIHTTPRequest

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

ASIHTTPRequest 是一款非常有用的 HTTP 访问开源项目,ASIHTTPRequest使用起来非常方便,可以有效减少代码量.他的功能非常强大,可以实现异步请求,队列请求,缓存,断点续传,进度跟踪,上传文件,HTTP 认证。同时它也加入了 Objective-C 闭包 Block 的支持.其官方网站: http://allseeing-i.com/ASIHTTPRequest/ 。

在使用ASI之前,需要导入一下框架:

    CFNetWork.framework
    SystemConfiguration.framework
    MobileCoreServices.framework
    libz.dylib

因为ASI是不支持ARC的,所以如果打算使用ARC功能,也就是arc和非arc混编,需要给非 ARC 模式的代码文件加入 -fno-objc-arc 标签。

 

一:ASIHTTPRequest实现上传文件功能

//上传
- (IBAction)UpLoad:(UIButton *)sender {
    NSURL *url=[NSURL URLWithString:@"http://localhost:8080/UploadServer/UploadServlet"];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"POST"];
    //方法一:主要应用与小文件,将小文件转换为Data类型,再将Data类型的数据放入post请求体中上传
    NSData *dataImage=UIImageJPEGRepresentation([UIImage imageNamed:@"1.jpg"], 1);
    [request appendPostData:dataImage];
    
    //方法二:直接从文件路径找到文件,并放入post请求体中上传,同样适用与小文件
    [request appendPostDataFromFile:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"]];
    
    //方法三:数据流方法(文件不分大小)
    //允许是从磁盘上读取数据,并将数据上传,默认为NO
    [request setShouldStreamPostDataFromDisk:YES];
    [request setPostBodyFilePath:[[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"]];
    
    //使用异步请求
    [request startAsynchronous];
}

 

 

一:ASIHTTPRequest实现文件断点下载

//下载
- (IBAction)downLoad:(UIButton *)sender {
    //判断请求是否为空.(如果双击开始,会产生一个不再受控制的请求)
    if (_request!=nil) {
        return;
    }
    //如果文件下载完毕就不再下载
    if ([[NSFileManager defaultManager]fileExistsAtPath:[self getFilePath]]) {
        return;
    }
    NSString *string=@"http://localhost:8080/downloadSrver/xcode_6.dmg.dmg";
    string=[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:string];
    //创建ASIHTTPRequest请求
    _request=[ASIHTTPRequest requestWithURL:url];
    //设置文件存放的目标路径
    [_request setDownloadDestinationPath:[self getFilePath]];
    [_request setDownloadProgressDelegate:self];
    //允许断点续传,必须设置
    [_request setAllowResumeForFileDownloads:YES];
    //设置大文件下载的临时路径
    //下载过程中文件一直保存在临时文件中,当文件下载完成后才移入目标路径下
    [_request setTemporaryFileDownloadPath:[self getTemoPath]];
    //设置代理
    _request.delegate=self;
    _request.downloadProgressDelegate=self;
    //异步请求
    [_request startAsynchronous];
}

- (IBAction)pause:(UIButton *)sender {
    //取消请求并清除代理
    [_request clearDelegatesAndCancel];
    _request=nil;
}
-(NSString *)getTemoPath
{
    //设置文件下载的临时路径
    NSString *string=[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp"];
    return string;
}
//获取沙盒
-(NSString *)getFilePath
{
    NSString *document=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath=[document stringByAppendingPathComponent:@"xcode6.dmg"];
    return filePath;
}
//获取下载进度
-(void)setProgress:(float)newProgress
{
    _progressVIew.progress=newProgress;
    _label.text=[NSString stringWithFormat:@"%.2f%%",newProgress*100];
}
//因为ASI是非ARC,所以系统不会自动释放,需要手动操作
-(void)dealloc
{
    [_request clearDelegatesAndCancel];
}