您的位置:首页 > 博客中心 > APP开发 >

iOS开发网络篇—网络请求(HTTP协议)小结(转)

时间:2022-03-11 03:18

1. 聊一下HTTP协议(协议的完整的通信过程)

 

2.通信过程

1> 请求

* 客户端 --> 服务器

* 请求的内容

a. 请求行(请求方法\HTTP协议\请求资源路径)

b. 请求头(描述客户端的信息)

c. 请求体(POST请求才需要有, 存放具体数据)

 

2> 响应

* 服务器 --> 客户端

* 响应的内容

a. 状态行(响应行, 状态码)

b. 响应头(服务器信息, 返回数据的类型, 返回数据的长度)

c. 实体内容(响应体, 返回给客户端的具体内容)

 

3.HTTP请求的方法

1> GET

* 参数都拼接在URL后面

* 参数有限制

 

2> POST

* 参数都在请求体

* 参数没有限制

 

4.iOS中发送GET\POST请求的手段

1> NSURLConnection

* 发送一个同步请求

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;

 

* 发送一个异步请求

+ (void)sendAsynchronousRequest:(NSURLRequest*) request

queue:(NSOperationQueue*) queue

completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;

 

* 代理的方法(异步)

[NSURLConnection connectionWithRequest:request delegate:self];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

 

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

[conn start];

相关推荐

电脑软件

本类排行

今日推荐

热门手游