iOS-Http : GET : POST,ios-httpgetpost
一.概述
* HTTP/1.1協議共定義了8中要求方法:OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.
* GET方法和POST是我們使用最頻繁的網路要求方法。
* GET和POST在應用場合有什麼區別呢?
* GET方法向指定資源發出請求,發送的訊息顯示的跟在URL後面,使用者資訊不安全,並且傳送資訊量有限。(如下所示,在請求中能看到使用者名稱和密碼)
http://localhost:8080/logandreg/logreg?name=wyg&pwd=1992
* 如果僅僅是向伺服器索要資料,沒有參數,使用GET比較方便。(如下所示)
http://www.baidu.com
* POST傳送的資訊量大,並且傳送的資訊是被隱藏的,傳送資訊比較安全,如果向伺服器傳送資料,建議使用POST.
二.GET請求網路資料(同步,非同步)
* 如上所述,GET方法可以向指定資源發出請求,比如我們想再網路上請求一張圖片在本地上顯示,使用GET方法就非常的方便。
* GET請求分為同步請求和非同步請求,一般情況下,為了良好的使用者體驗,我們都使用非同步請求。
* GET同步請求一張網狀圖片(程式碼摺疊功能)
1 NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];2 NSURLRequest *request = [NSURLRequest requestWithURL:url];3 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];4 _iamgeview.image = [UIImage imageWithData:data];同步擷取網狀圖片
*GET非同步請求一張網狀圖片(iOS5.0)(程式碼摺疊功能):
1 NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];2 NSURLRequest *request = [NSURLRequest requestWithURL:url];3 NSOperationQueue *queue = [[NSOperationQueue alloc]init];4 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {5 _iamgeview.image = [UIImage imageWithData:data];6 }];非同步擷取網狀圖片
*GET非同步請求(iOS2.0)(程式碼摺疊功能)
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"]; 5 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 6 [NSURLConnection connectionWithRequest:request delegate:self]; 7 } 8 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 9 {10 NSLog(@"接收到響應");11 _buffer = [[NSMutableData alloc]init];12 }13 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data14 {15 NSLog(@"接收到資料");16 [_buffer appendData:data];17 }18 - (void)connectionDidFinishLoading:(NSURLConnection *)connection19 {20 NSLog(@"請求完成");21 _iamgeview.image = [UIImage imageWithData:_buffer];22 }23 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)erro24 {25 NSLog(@"請求出錯");26 }非同步請求網狀圖片
三.POST請求網路資料(以非同步為例,同步的與GET類似)
* 傳輸資訊安全性比GET高。
* 傳輸資訊量比GET大。
* 代碼中帶有詳細解釋,代碼如下:
- (IBAction)postRequest:(id)sender{ //明確請求的url NSURL *url = [NSURL URLWithString:@"http://localhost:8080/logandreg/logreg"]; //建立請求(可變請求) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //指定請求方式 [request setHTTPMethod:@"POST"]; //拼接參數內容 NSString *body = @"name=wyg&pwd=1992"; //請求資料放到請求的請求體中 [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]]; //使用post發起非同步請求 [NSURLConnection connectionWithRequest:request delegate:self];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ //接收到響應之後響應的方法 _buffer = [[NSMutableData alloc]init];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ //接收到資料之後響應的方法 [_buffer appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ //資料處理完成之後響應的方法 NSString *str = [[NSString alloc]initWithData:_buffer encoding:NSUTF8StringEncoding]; NSLog(@"%@",str);}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //請求出錯之後響應的方法(如斷網,逾時)}