* 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同步請求一張網絡圖片(代碼折疊)

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{ //請求出錯之后響應的方法(如斷網,超時)}
新聞熱點
疑難解答