原文来源:
http://afantihust.blog.51cto.com/2231549/533691
同步请求代码
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@" http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
异步请求代码
//发起请求,
- (void)newOrderShowRequest:(NSString *)requestInfo {
NSURL *url = [NSURL URLWithString:newOrderShowLink];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//设置处理返回结果代理函数,不设置则默认为requestFinished
[request setDidFinishSelector:@selector(newOrderShowRequestFinished:)];
//设置处理返回错误代理函数,不设置则默认为requestFailed
[request setDidFailSelector:@selector(newOrderShowRequestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
//处理返回结果
- (void)newOrderShowRequestFinished:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
NSData *responseData = [request responseData];
}
//处理返回错误
- (void)newOrderShowRequestFailed:(ASIHTTPRequest *)request {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"连接失败" message:@"请检查网络连接." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
ASIHttpRequest开源包,封装iPhone/iPad上发送接收Http请求,官网地址
http://allseeing-i.com/ASIHTTPRequest/
ASIHTTPRequest需要从网上下载。Mac os,Xcode系统没有自带;
支持功能:
1. 下载的数据直接保存到内存或文件系统里
2. 提供直接提交(HTTP POST)文件的API
3. 可以直接访问与修改HTTP请求与响应HEADER
4. 轻松获取上传与下载的进度信息
5. 异步请求与队列,自动管理上传与下载队列管理机
6. 认证与授权的支持
7. Cookie
8. 请求与响应的GZIP
9. 代理请求
1. 下载的数据直接保存到内存或文件系统里
2. 提供直接提交(HTTP POST)文件的API
3. 可以直接访问与修改HTTP请求与响应HEADER
4. 轻松获取上传与下载的进度信息
5. 异步请求与队列,自动管理上传与下载队列管理机
6. 认证与授权的支持
7. Cookie
8. 请求与响应的GZIP
9. 代理请求
使用方式:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
添加以下文件到项目(若请求只需要哪些文件也可选择添加)
ASIHTTPRequestConfig.h
ASIHTTPRequestDelegate.h
ASIProgressDelegate.h
ASICacheDelegate.h
ASIHTTPRequest.h
ASIHTTPRequest.m
ASIDataCompressor.h
ASIDataCompressor.m
ASIDataDecompressor.h
ASIDataDecompressor.m
ASIFormDataRequest.h
ASIInputStream.h
ASIInputStream.m
ASIFormDataRequest.m
ASINetworkQueue.h
ASINetworkQueue.m
ASIDownloadCache.h
ASIDownloadCache.m
iPhone projects must also include:
ASIAuthenticationDialog.h
ASIAuthenticationDialog.m
Reachability.h (in the External/Reachability folder)
Reachability.m (in the External/Reachability folder)
CFNetwork.framework,
SystemConfiguration.framework,
MobileCoreServices.framework,
CoreGraphics.framework,
libz.1.2.3.dylib
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
添加以下文件到项目(若请求只需要哪些文件也可选择添加)
ASIHTTPRequestConfig.h
ASIHTTPRequestDelegate.h
ASIProgressDelegate.h
ASICacheDelegate.h
ASIHTTPRequest.h
ASIHTTPRequest.m
ASIDataCompressor.h
ASIDataCompressor.m
ASIDataDecompressor.h
ASIDataDecompressor.m
ASIFormDataRequest.h
ASIInputStream.h
ASIInputStream.m
ASIFormDataRequest.m
ASINetworkQueue.h
ASINetworkQueue.m
ASIDownloadCache.h
ASIDownloadCache.m
iPhone projects must also include:
ASIAuthenticationDialog.h
ASIAuthenticationDialog.m
Reachability.h (in the External/Reachability folder)
Reachability.m (in the External/Reachability folder)
CFNetwork.framework,
SystemConfiguration.framework,
MobileCoreServices.framework,
CoreGraphics.framework,
libz.1.2.3.dylib
同步请求代码
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@" http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
异步请求代码
//发起请求,
- (void)newOrderShowRequest:(NSString *)requestInfo {
NSURL *url = [NSURL URLWithString:newOrderShowLink];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//设置处理返回结果代理函数,不设置则默认为requestFinished
[request setDidFinishSelector:@selector(newOrderShowRequestFinished:)];
//设置处理返回错误代理函数,不设置则默认为requestFailed
[request setDidFailSelector:@selector(newOrderShowRequestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
//处理返回结果
- (void)newOrderShowRequestFinished:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
NSData *responseData = [request responseData];
}
//处理返回错误
- (void)newOrderShowRequestFailed:(ASIHTTPRequest *)request {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"连接失败" message:@"请检查网络连接." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
通过设置处理返回结果/错误代理函数即可处理同时发起的多个请求,如果需要更规范的管理发起的多个请求可以使用队列,代码详见官方例子