第一部分:基础网络学习
(1)用NSURLConnection获取网络数据以及判断网络是否连接成功
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc]initWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(!connectionError)
{
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
}];
(2)同步请求
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc]initWithURL:url] returningResponse:&response error:&error];
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"%@",response.MIMEType);
NSLog(@"%@",response.textEncodingName);
(3)使用delegate的方式请求数据
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[[NSURLConnection alloc]initWithRequest:[[NSURLRequest alloc]initWithURL:url] delegate:self];
以上内容都能够获得基本的网络数据,但是如果又复杂的网络操作,比如下载比较大的文件或者上传比较大的文件,这些东西都会显得力不从心,所以复杂的网络操作推荐下面的框架
第二部分:ASIHTTPRequest基本学习
1.引入文件
2.需要添加的包有
3.简单应用,用于显示百度网页的内容,ASIHTTPRequest请求的方式也有两种同步和异步的
(1)简单的同步请求(Synchronous),代码如下
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if(!error)
{
NSString *response = [request responseString];
NSLog(@"%@",response);
}
(2)简单的异步请求(Asynchronous)比较简单
(3)配合NSOperationQueue进行队列操作
-(void)addUrlToQueque:(NSString*)urlString
{
[array addObject:[[NSURL alloc]initWithString:urlString]];
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"----------------\n");
NSLog(@"%@",[request responseString]);
NSLog(@"----------------\n");
}
-(void)requestStarted:(ASIHTTPRequest *)request
{
NSLog(@"start");
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"error-----");
}
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
array = [[NSMutableArray alloc] init];
queue.maxConcurrentOperationCount = 4;
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
[self addUrlToQueque:@"http://www.baidu.com"];
for (int i = 0; i<array.count; i++) {
ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[array objectAtIndex:i]];
[request setDelegate:self];
[request setTag:i];
[queue addOperation:request];
}
,请求的自定义方法,当然需要用到selector了
[request setDidFinishSelector:@selector(rm1:)];
[request setDidFailSelector:@selector(rm2:)];
不要使用request的URL来区分每个request,因为URL可能会改变(例如重定向),如果需要使用request的URL,使用[request originalURL],这个将永远返回第一个url。
但是可以选择设定request的userInfo属性,它是个NSDictionary,或者更简单的方法是设定每个request的tag属性,这两个属性都不会被发送到服务器。
(4)ASINetworkQueue是NSOperationQueue的子类,提供更高级的特性(ASINetworkQueue的代理函数):
- requestDidStartSelector
当一个request开始执行时,这个代理函数会被调用。 - requestDidReceiveResponseHeadersSelector
当队列中的request收到服务器返回的头信息时,这个代理函数会被调用。对于下载很大的文件,这个通常比整个request的完成要早。 - requestDidFinishSelector
当每个request完成时,这个代理函数会被调用。 - requestDidFailSelector
当每个request失败时,这个代理函数会被调用。 - queueDidFinishSelector
当队列完成(无论request失败还是成功)时,这个代理函数会被调用。
ASINetworkQueues与NSOperationQueues稍有不同,加入队列的request不会立即开始执行。
(5)利用ASIHTTPRequest向服务器发送信息
复习下http协议
a.头信息
典型的请求消息:
GET http://download.microtool.de:80/somedata.exe
Host: download.microtool.de
Accept:*/*
Pragma: no-cache
Cache-Control: no-cache
Referer: http://download.microtool.de/
User-Agent:Mozilla/4.04[en](Win95;I;Nav)
Range:bytes=554554-
第一行表示HTTP客户端(可能是浏览器、下载程序)通过GET方法获得指定URL下的文件。
其中Pragma: no-cache ,Cache-Control: no-cache是公共头信息
Host头域
Host头域指定请求资源的Intenet主机和端口号,必须表示请求url的原始服务器或网关的位置。HTTP/1.1请求必须包含主机头域,否则系统会以400状态码返回。
Referer头域
Referer头域允许客户端指定请求uri的源资源地址,这可以允许服务器生成回退链表,可用来登陆、优化cache等。他也允许废除的或错误的连接由于维护的目的被追踪。如果请求的uri没有自己的uri地址,Referer不能被发送。如果指定的是部分uri地址,则此地址应该是一个相对地址。
User-Agent头域
User-Agent头域的内容包含发出请求的用户信息。
响应消息
典型的响应消息:
HTTP/1.0200OK
Date:Mon,31Dec200104:25:57GMT
Server:Apache/1.3.14(Unix)
Content-type:text/html
Last-modified:Tue,17Apr200106:46:28GMT
Etag:"a030f020ac7c01:1e9f"
Content-length:39725426
Content-range:bytes554554-40279979/40279980
第二部内容参考:http://mobile.51cto.com/iphone-405168.htm