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

【Objective-C】Http常用API、同步请求与异步请求

2015-05-04 12:02 113 查看
开发iOS应用要调用Http接口、获取Http资源,有一套比较成熟的框架ASIHTTPRequest。而我还是比较喜欢使用原始一点的API,而它跟其他的面向对象语言有许多共通之处。本文分同步请求和异步请求这两种情况来讲解一下Http API的使用。直接上代码,注释即文档!

同步请求:即发起Http请求、获取并处理返回值都在同一个线程中进行

[objc] view
plaincopy





//创建URL对象

NSString *urlStr = @"http://blog.csdn.net/rongxinhua";

NSURL *url = [[NSURL alloc] initWithString:urlStr];

//创建HTTP请求

//方法1(注:NSURLRequest只支持Get请求,NSMutableURLRequest可支持Get和Post请求)

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

//方法2,使用工厂方法创建

NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//同时设置缓存策略和超时时间

NSMutableURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];

//设置Http头

NSDictionary *headers = [request allHTTPHeaderFields];

[headers setValue:@"iOS-Client-ABC" forKey:@"User-Agent"];

//设置请求方法

[request setHTTPMethod:@"GET"];

[request setHTTPMethod:@"POST"];

//设置要发送的正文内容(适用于Post请求)

NSString *content = @"username=stanyung&password=123";

NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];

//同步执行Http请求,获取返回数据

NSURLResponse *response;

NSError *error;

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//返数据转成字符串

NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

//(如果有错误)错误描述

NSString *errorDesc = [error localizedDescription];

//获取状态码和HTTP响应头信息

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

NSInteger statusCode = [httpResponse statusCode];

NSDictionary *responseHeaders = [httpResponse allHeaderFields];

NSString *cookie = [responseHeaders valueForKey:@"Set-Cookie"];

注:以上代码,不要Copy直接执行,只是列举Http常用方法的调用。

异步请求:发起HTTP请求在一个线程中,返回结果处理在另一个线程中。相比同步请求,异步请求不需要等待返回结果,当前程序可以继续往下执行。在Objective-C中,异步请求也有两种实现方式:一种是注册回调代理,一种是使用回调代码块。

a.注册回调代理的方式:

[objc] view
plaincopy





[NSURLConnection connectionWithRequest:request delegate:self];

需要实现NSURLConnectionDataDelegate协议:

[objc] view
plaincopy





@interface HttpDownloadService : NSObject<NSURLConnectionDataDelegate> {

}

实现相关的协议方法:

[objc] view
plaincopy





NSMutableData *buff; //暂存响应的数据

bool finished = false; //读完完整标记

//收到HTTP响应时调用

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;

NSDictionary *headers = [httpResponse allHeaderFields];

buff = [[NSMutableData alloc] init];

}

//读取返回数据时调用(可能会执行多次此方法)

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[buff appendData:data];

}

//读完数据完成时调用

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString *html = [[NSString alloc] initWithData:buff encoding:NSUTF8StringEncoding];

finished = true;

}

通常情况下,数据在网络中传输,会受到带宽等因素的影响,并不会一次情将所有数据返回,你可能分几次才能接受完整一个HTTP响应报文。因此,(void)connection:(NSURLConnection *)didReceiveData:(NSData *) 这个方法很可能会执行多次。

上例代码中,使用了NSMutableData来暂存接收到的响应数据片段,每一段并接起来,直到读取完整。

b.使用回调代码块的方式:

[objc] view
plaincopy





[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:

^(NSURLResponse *response, NSData *result, NSError *error){ //只会进入一次,方法内部已经实现了Buffer作用

NSString *html = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

}];

跟NSURLConnectionDataDelegate的回调方法不同,这里的回调代码块只调用一次,它内部已经实现了Buffer的作用,等到数据接收完整后才进入此代码块。因此,你在代码块中获取到的result可以直接使用。

备注1:本文的代码例子将使用ARC编码模式,故所新建的对象均没有显式调用release()方法回收。

备注2:若你测试本例子代码新建的是Command Line Tool工程,在main函数中执行相关代码,上面两种异步执行的情况,你很可能你的程序没有执行到回调方法或回调代码块里面去,这是因为:在main函数中,主线程没有等待阻塞,一下子执行完了,回调代码所在的子线程可能未执行完或者根本还没开始执行,就已经因为主线程的结束而结束了。解决这个问题,可以在调用完异步方法后面,加以下代码:

[objc] view
plaincopy





while (!finished) {

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}

而finished变量正是上面两个异步HTTP例子中定义是否执行完成的变量。

@容新华技术博客 - http://blog.csdn.net/rongxinhua - 原创文章,转载请注明出处
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: