国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS-Http:GET:POST

2019-11-14 18:19:25
字體:
來源:轉載
供稿:網友

一.概述

* 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{    //請求出錯之后響應的方法(如斷網,超時)}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤山市| 屏东县| 易门县| 都兰县| 花莲市| 资源县| 青田县| 芦溪县| 姜堰市| 城固县| 兴文县| 南宁市| 含山县| 聂拉木县| 雅江县| 镇赉县| 涟水县| 防城港市| 凤阳县| 庄河市| 盐亭县| 孟津县| 临江市| 天全县| 布尔津县| 边坝县| 文登市| 永善县| 曲水县| 扬州市| 皋兰县| 宜州市| 永川市| 屏东县| 眉山市| 贺州市| 碌曲县| 湘阴县| 九台市| 嵊泗县| 金昌市|