標籤:
iOS開發網路篇—網路請求(HTTP協議)小結
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];
iOS開發網路篇—網路請求(HTTP協議)小結