IOS---网络以及框架ASIHTTPRequest入门学习
发布日期:2021-11-21 04:40:59 浏览次数:29 分类:技术文章

本文共 4273 字,大约阅读时间需要 14 分钟。

第一部分:基础网络学习

(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
,请求的自定义方法,当然需要用到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

转载地址:https://blog.csdn.net/xiaoji94shen/article/details/21153729 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:IOS---文件路径相关问题
下一篇:IOS---NSThread同步处理

发表评论

最新留言

不错!
[***.144.177.141]2024年04月23日 06时57分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

app运行提示Unable to Initialize Unity Engine 2019-04-27
spring boot 与 Ant Design of Vue 实现修改按钮(十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除按钮(十八) 2019-04-27
spring boot 与 Ant Design of Vue 实现角色管理布局以及角色的列表(十九) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除角色(补二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二) 2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2019-04-27
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除用户(三十) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27