網絡請求小結服務器
1. HTTP協議(協議的完整的通訊過程)網絡
2.通訊過程異步
1> 請求代理
* 客戶端 --> 服務器資源
* 請求的內容同步
a. 請求行(請求方法\HTTP協議\請求資源路徑)it
b. 請求頭(描述客戶端的信息)io
c. 請求體(POST請求才須要有, 存放具體數據)request
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];