網絡分層結構.png
http 通信流程.png
一 GET請求和POST請求的區別
GET請求和POST請求的區別主要是體現在數據的傳遞上.
GET請求相對于POST請求是不安全的
-
GET請求:
在請求的URL后面一 '?'的形式跟上發給服務器的參數,多個參數之間以'&'隔開比如:http://www.baidu.com/login?usrName=zhangsan&pwd=123456&type=json
由于瀏覽器和服務器對URL的長度有限制,因此在URL后面附帶參數是由限制的,通常不超多1kb.
POST 請求
發送個服務器的參數全部放在請求體中
理論上POST請求體傳遞的數據是沒有限制的
二 HTTP請求過程----請求
HTTP 協議規定: 一個完整的由客戶端發送給服務器的HTTP 請求中包含以下內容.
- 請求頭 包含了 對客戶端的環境描述,客戶端的請求信息等.
GET /xiaoming.png HTTP/1.1 // 包含了請求方法 請求路徑 HTTP協議的版本
Host: 120.25.226.186:12345 // 客戶端想訪問服務器的主機地址
User-Agent: Mozilla/5.0 //客戶端的類型,客戶端的軟件環境
Accept: text/html // 客戶端所能接受的數據類型
Accept-Language: zh cn //客戶端語言環境
Accept-Encoding: gzip //客戶端支持的數據壓縮格式
- 請求體: 客戶端 發送給服務器的具體數據,比如文件數據. (請求體不是必須的,比如GET請求)
二 HTTP請求過程----相應
- 客戶端 向服務器 發送請求,服務器應當做出相應,即返數據給客戶端
- HTTP協議規定: 1個完整的相應體中應包含以下幾個部分:
響應頭:包含對服務器的描述,對返回數據的描述
響應體 服務器返給客戶端的具體數據,比如文件數據HTTP/1.1 200 ok //http 的版本 狀態 Server: Apache-Coyote/1.1 // 服務器的類型 Content-Type: image/jpeg // 返回數據的類型 Content-Length: 56811 //返回數據的長度 Date: Mon jun 2015 12:29:53 GMT // 相應時間
三 HTTP請求狀態碼類別含義
類別 | 原因短語 | 解釋 | 比如 |
---|---|---|---|
1xx開頭 | 信息性狀態碼 | 接收的請求正在處理 | |
2xx開頭 | 成功狀態碼 | 請求正常處理完畢 | 比如:200 表示成功 |
3xx開頭 | 重定向狀態碼 | 要完成附加請求操作以完成請求 | |
4xx開頭 | 客戶端錯誤 | 客戶端錯誤 | 比如:404 請求路徑錯誤,客戶端傳錯了 |
5xx開頭 | 服務端錯誤 | 服務端錯誤 | 比如:500 服務器出錯 |
四 iOS中HTTP GET 請求
- NSURLConnection 發送同步請求 GET
-(void)sendsync{
// 1. 確定請求路徑 (GET 請求: 基礎路勁 + 參數)
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:12345/logo?usr=zhangsan&pwd=123456"];
// 2. 創建請求對象 (該對象內部會默認 生成請求頭), GET 請求沒有請求體
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 發送請求
NSURLResponse *response = nil; // 響應頭
NSError *err = nil;
// 4.返回請求的響應數據
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"data : %@",data);
}
- NSURLConnection 發送異步請求 GET
- (void)sendAsync{
// 1. 確定請求路徑 (GET 請求: 基礎路勁 + 參數)
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:12345/logo?usr=zhangsan&pwd=123456"];
// 2. 創建請求對象 (該對象內部會默認 生成請求頭), GET 請求沒有請求體
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 發送請求synchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { // 響應數據
NSLog(@"response: %@, data: %@,connectionError : %@",response, data, connectionError);
}];
}
- NSURLConnection 發送請求 代理接收響應數據 GET
@interface TestViewController ()<NSURLConnectionDataDelegate>
@end
@implementation TestViewController
- (void)sendAsyncDelegate{
// 1. 確定請求路徑 (GET 請求: 基礎路勁 + 參數)
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:12345/logo?usr=zhangsan&pwd=123456"];
// 2. 創建請求對象 (該對象內部會默認 生成請求頭), GET 請求沒有請求體
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 發送請求
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//startImmediately:NO 時,不會立即發送請求需要手動調用start 發送請求
//NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
//[connection start];
}
#pragma mark- NSURLConnectionDataDelegate
// 1 接收到相應頭調用
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
}
//2.接收到數據調用, 該方法可能被調用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
}
// 3. 完成后調用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
}
//4. 失敗時調用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
}
五 iOS中HTTP POST 請求
- (void)sendAsyncPOST{
// 1. 確定請求路徑 (GET 請求: 基礎路勁 + 參數)
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:12345/logo"];
// 2. 創建請求對象 (該對象內部會默認 生成請求頭)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1如果請求頭的信息不滿足可以自己修改
[request setValue:@"ios 10.2" forHTTPHeaderField:@"User-Agent"];
// 2.2設置請求超時時間
request.timeoutInterval = 15;
//3.設置請求方式
request.HTTPMethod = @"POST";
//4.設置請求體
request.HTTPBody = [@"usr=zhangsan&pwd=123456" dataUsingEncoding:NSUTF8StringEncoding];
// 3. 發送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// 處理相應信息
}];
}
五 URL轉碼
在使用URL的時候,如果字符串中設計到中文,那么需要對中文進行轉碼
NSString *urlStr = @"http://120.25.226.186:12345/logo?usr=張三&pwd=12345";
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];