网络请求(HTTP协议)小结

1. 聊一下HTTP协议(协议的完整的通讯过程)服务器

 

2.通讯过程异步

1> 请求代理

* 客户端 --> 服务器资源

* 请求的内容同步

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

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

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

 

2> 响应请求

* 服务器 --> 客户端queue

* 响应的内容

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];  

相关文章
相关标签/搜索