HTTP常見的請求GET&POST

GET和POST是兩種常見的與服務器進行交互的HTTP方法:html

1.GET:api

.GET從語意上是獲取指定URL的資源服務器

.傳輸過程當中數據被放在請求的URL中。session

.傳輸的數據量小,這主要是由於受URL的長度限制。app

2.POSTpost

.POST從語意上是向指定URL的資源添加(提交)數據url

.將數據放在數據體(請求體),按照變量和值相對應的方式,傳遞到spa

action所指向URL線程

.全部數據對用戶來講不可見。code

.能夠傳輸大量數據,上傳文件只能使用POST

 

3.補充:

.HTTP請求中主要由兩部分組成:http請求頭,http請求體

POST請求才有請求體。

4.下面我將詳細介紹如何設置請求頭和請求體

如何取得響應頭和響應體,以及如何監聽數據傳輸事件。

5.首先get請求主要包含如下幾個步驟:

1>構造URL

2>構造Request

3>配置session會話對象

4>構造session,發送會話請求

viewController中 1 - (IBAction)getAction:(id)sender {

 2     
 3     //1.構造URL
 4     NSString *stringURL = @"https://api.douban.com/v2/movie/us_box";  5     NSURL *url = [NSURL URLWithString:stringURL];  6     
 7     //2.構造Request  8     //通常使用可變的
 9     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 10     //<1>設置請求方式,默認就是GET請求
11     [request setHTTPMethod:@"GET"]; 12     //<2>設置超時時間
13     [request setTimeoutInterval:30]; 14     
15     //<3>設置請求頭 16 // request setAllHTTPHeaderFields:<#(NSDictionary *)#> 17 // [request setValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>] 18 // [request addValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>] 19     
20     //<4>設置請求體 21 // [request setHTTPBody:<#(NSData *)#>] 22     
23     //4.構造session,發送會話請求
24     NSURLSession *session = [NSURLSession sharedSession]; 25     
26     NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 27         
28         if (error == nil) { 29             //data 就是響應體 30             // response 能夠獲取響應頭 31             // NSLog(@"data:%@",data); 32             
33             //<1>獲取響應體 34             //將響應的數據轉換成字符串
35             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 36             //錯誤,須要回到主線程操做UI 37             // _textView.text = str;
38             
39  [_textView performSelectorOnMainThread:@selector(setText:) withObject:str waitUntilDone:NO]; 40             
41             //<2>獲取響應頭
42             NSHTTPURLResponse *r = (NSHTTPURLResponse *)response; 43             NSDictionary *header = r.allHeaderFields; 44             NSLog(@"header:%@",header); 45  } 46  }]; 47     
48     //開始任務
49  [task resume]; 50     
51 //
52     
53 }

6.POST請求:

/*

示例:的URL及相對應的請求參數

 POST請求參數:movie_id=43485

 http://piao.163.com/m/movie/detail.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000

 */

- (IBAction)postAction:(id)sender {
    
    //1.構造URL
    NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/movie/detail.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"];
    
    //2.構造Request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    
    //設置一些信息
    //<1>設置請求方式
    [request setHTTPMethod:@"POST"];
    //<2>設置請求超時時間
    [request setTimeoutInterval:30];
    //<3>設置請求頭
//    [request setAllHTTPHeaderFields:<#(NSDictionary *)#>]

//    [request addValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>]
//    [request setValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>]

    //<4>設置請求體
    NSString *bodyString = @"movie_id=43485";
    NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    //3.建立配置session的對象
    //4.構造session
    NSURLSession *session = [NSURLSession sharedSession];
    
    //連接服務器
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (error == nil) {
            
            //取得響應體
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            _textView.text = str;
            [_textView performSelectorOnMainThread:@selector(setText:) withObject:str waitUntilDone:YES];
            
            //取得響應頭
            NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
            NSDictionary *dic = urlResponse.allHeaderFields;
            NSLog(@"dic:%@",dic);
        }
        
    }];
    
    //開始任務
    [task resume];
    
}

POST相對與GET步驟都差很少相同,只不過POST須要設置請求體。

相關文章
相關標籤/搜索