iPhone发送接收Http请求——ASIHttpRequest 绝对好东西
来源:互联网 发布:桌面高考倒计时软件 编辑:程序博客网 时间:2024/06/10 22:07
原文来源: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
简单示例:
http://allseeing-i.com/ASIHTTPRequest/How-to-use
http://allseeing-i.com/ASIHTTPRequest/How-to-use
同步请求代码
- (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];
}
通过设置处理返回结果/错误代理函数即可处理同时发起的多个请求,如果需要更规范的管理发起的多个请求可以使用队列,代码详见官方例子
- iPhone发送接收Http请求——ASIHttpRequest 绝对好东西
- HttpURLConnection发送接收Http请求
- cocos2dx之Http网络编程(一)——网络请求的发送和接收
- cocos2dx之Http网络编程(一)——网络请求的发送和接收
- cocos2dx之Http网络编程(一)——网络请求的发送和接收
- [NIO]用dawn发送接收HTTP请求
- ASIHTTPRequest 网络请求 iphone
- ASIHTTPRequest 网络请求 iphone
- 绝对的好东西
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- IOS开发之——ASIHTTPRequest请求时,发送了2次请求问题解决
- IOS开发之——ASIHTTPRequest请求时,发送了2次请求问题解决
- ASIHTTPRequest发送异步GET请求
- Project——通过Http发送请求
- windows网络——发送http请求
- howto使用http接收和发送简单的xml请求
- Android HTTP实例 发送请求和接收响应
- Android HTTP实例 发送请求和接收响应
- linux知识点
- 信号与槽
- Android开发环境配置注意事项
- 非对称加密
- 2011-12-8工作进展
- iPhone发送接收Http请求——ASIHttpRequest 绝对好东西
- Coin Test n 204 郑州大学第四次ACM比赛试题
- 51单片机串口通信时遇到的问题
- struts2所必须的jar包
- SSH读取applicationContext.xml配置文件测试hibernate方法
- 正确的删除tc的filter过滤器
- 我的2011--虚荣、挣扎、总结和转变的一年
- ubuntu下安装jdk6.0(整理自互联网)
- iPhone开发经验教训总结