关于ASIHTTPRequest的使用总结

本文介绍如何使用ASIHTTPRequest和ASIFormDataRequest进行文件的上传和下载,包括设置请求头、处理响应及异常情况。
  • 分享类型:应用开发相关
使用ASIHTTPRequest可以很方便的进行一下操作: 
  • 同步/异步方式下载数据
  • 定义下载队列,让队列中的任务按指定的并发数来下载(队列下载必须是异步的)
  • 提交表单,文件上传
  • 处理cookie
  • 设置代理
  • 上下载进度条
  • 重定向处理
  • 请求与响应的GZIP
  • 验证与授权
(1)使用中经常用到的是ASIHTTPRequest以及ASIFormDataRequest 
 
 
ASIHTTPRequest用于一般的网络请求、下载等都没有问题,ASIFormDataRequest更加的适用于带有参数的网络请求,当然ASIHTTPRequest也能够实现; 
 
 
(2)使用ASIHTTPRequest进行下载 
 
 
ASIHTTPRequest * downloadRequest = [ASIHTTPRequest requestWithURL:url]; 
 
//设置目的地址 
    [downloadRequest setDownloadDestinationPath:path]; 
// 设置临时地址 
    [downloadRequest setTemporaryFileDownloadPath:[path stringByAppendingString:@".tmpDLFile"]]; 
    //保存下载文件title 
    downloadRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:fileTitle,@"fileTitle", nil]; 
//设置委托 
    [downloadRequest setDelegate:self]; 
//设置是是否支持断点下载 
    [downloadRequest setAllowResumeForFileDownloads:YES]; 
 
//支持后台 
    [downloadRequest setShouldContinueWhenAppEntersBackground:YES]; 
//完成后调用 
    [downloadRequest setDidFinishSelector:@selector(downloadFinish:)]; 
 
    [downloadRequest setDidFailSelector:@selector(downfailed:)]; 
 
    [downloadRequest startAsynchronous];

3)使用ASIHTTPRequest上传文件(例子google语音识别)  
 
 
- (void)sendRequest:(id)sender {   
    NSURL *URL = [NSURL URLWithString:@"http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=9"];   
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc]initWithURL:URL] autorelease];   
 
    [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];   
    [request addRequestHeader:@"Content-Type" value:@"audio/x-flac; rate=16000"];   
 
    [request setRequestMethod:@"POST"];   
 
    NSData *data = [NSData dataWithContentsOfFile:@"/Users/adminadmin/Desktop/hello.flac"];   
    NSLog(@"date:%@",data);   
    [request appendPostData:data];   
    [request setDidFinishSelector:@selector(didFinishPost:)];   
    [request setDidFailSelector:@selector(didFailedPost:)];   
 
    [request setDelegate:self];   
    [request startSynchronous];   
 
(4)使用ASIFormDataRequest 上传带有参数的请求 
 
 
- (void) upload{ 
 
NSString *postURLString = [NSString stringWithFormat:@"http://....?method=SaveData"]; 
NSURL *URL = [[[NSURL alloc]initWithString:postURLString] autorelease]; 
 
ASIFormDataRequest *postRequest = [[[ASIFormDataRequest alloc]initWithURL:URL] autorelease]; 
NSLog(@"url:%@",postRequest.url); 
 
 
     postRequest.defaultResponseEncoding = NSUTF8StringEncoding;  
 
    [postRequest addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 
    [postRequest addRequestHeader:@"Content-Type" value:@"charset=utf-8"]; 
 
 
    [postRequest setPostValue:value1 forKey:@"subject"]; 
    [postRequest setPostValue:value2 forKey:@"content"]; 
 
    [postRequest setDelegate:self]; 
    [postRequest setTimeOutSeconds:180.0]; 
    [postRequest setDidFinishSelector:@selector(didFinishPost:)]; 
    [postRequest setDidFailSelector:@selector(didFailPost:)]; 
    [postRequest startAsynchronous]; 
 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值