强大的HTTP包装开源项目ASIHTTPRequest介绍
ASIHTTPRequest 是一个直接在CFNetwork上做的开源项目,提供了一个比官方更方便更强大的HTTP网络传输的封装。它的特色功能如下:
1,下载的数据直接保存到内存或文件系统里
2,提供直接提交(HTTP POST)文件的API
3,可以直接访问与修改HTTP请求与响应HEADER
4,轻松获取上传与下载的进度信息
5,异步请求与队列,自动管理上传与下载队列管理机
6,认证与授权的支持
7,Cookie
8,请求与响应的GZIP
9,代理请求
源码:http://www.jinhusns.com/Products/Download/?type=xcj
下面来两个ASIHTTPRequest的小例子:
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request start];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
当你需要添加更多的请求信息时,如,添加个请求Header:
[request addRequestHeader:@"name" value:@"Jory lee"];
添加Post请求时的健值:
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
设置HTTP的授权帐号:
[request setUsername:@"username"];
[request setPassword:@"password"];
一个异步请求:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
在我们数据获取的过程中,如果数据源复杂,一个请求队列是必不可少的:
- (IBAction)grabURLInTheBackground:(id)sender
{
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
另外大家可以通过下面这个代码例子,看 ASIHTTP 比传统的 post 方法的方便之处
post用法
NSString *post = @"这里放要传递的参数";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"请求地址"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//[request addValue:@"gzip" forHTTPHeaderField:@"Accepts-Encoding"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedbData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
ASIHTTP 方法
NSURL *url = [NSURL URLWithString:@"请求地址"];
//ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"值" forKey:@"参数1"];
[request setPostValue:@"值" forKey:@"参数2"];
[request start];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(response);
}
相比之下,ASIHTTP 的确省了很多代码。