您的位置:首页 > 理论基础 > 计算机网络

ASIHTTPRequest 简单使用

2017-08-03 15:37 369 查看
ASIHTTPResquest 框架功能强大,应用非常多。

曾经写过ASIHTTPResquest的导入,如今就看一下基本使用

记一下当中基础的操作;

1、发送同步请求;

NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];//构造url字符串
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];//构造请求对象
[request startSynchronous];//開始同步请求 <span style="font-family: Arial, Helvetica, sans-serif;">startSynchronous 指的是同步</span>
NSString *response = [request responseString];//获取请求字符串
NSLog(@"%@",response);


2、发送异步请求

NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startAsynchronous];
[request setDelegate:self];//设置托付
NSString *response = [request responseString];
NSLog(@"%@",response);


异步请求的delegate;

与同步请求不同异步请求须要拦截HTTP会话事件。并将事件托付给代理来处理。

托付:ASIHTTPRequestDelegate

-(void)requestStarted:(ASIHTTPRequest *)request
{
//请求開始的时候调用
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
//请求完毕的时候调用
}
-(void) requestFailed:(ASIHTTPRequest *)request
{
//请求失败的时候调用
}
-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
//收到HTTP头的时候调用
}


还有非常多托付方法能够点进去看一下。

ASIHTTPResquest还支持 块

用块就不须要实现托付了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: