iOS開發中的網路請求

2021-06-25 19:11:05 字數 3902 閱讀 1480

今天又開啟了好久沒寫的部落格,看了一下日期,距離上一次寫部落格正好乙個月,這乙個月,又學到了好多關於ios開發的知識, 今天就來說說關於ios開發過程中的網路請求。

關於網路請求的重要性我想不用多說了吧。對於移動客戶端來說,網路的重要性不言而喻。常見的網路請求有同步get, 同步post, 非同步get, 非同步post。今天來看一下四種網路請求的實現方式。

一、同步get

// 1.將**初始化成乙個oc字串物件

nsstring *urlstr = [nsstring stringwithformat:@"%@?query=%@®ion=%@&output=json&ak=6e823f587c95f0148c19993539b99295", kbusinessinfourl, @"銀行", @"濟南"];

// 如果**中存在中文,進行urlencode

nsstring *newurlstr = [urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

// 2.構建網路url物件, nsurl

nsurl *url = [nsurl urlwithstring:newurlstr];

// 3.建立網路請求

nsurlrequest *request = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:10];

// 建立同步鏈結

nsurlresponse *response = nil;

nserror *error = nil;

nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];

當建立好同步鏈結以後, 就可以採用相應的方法進行解析。下面建立非同步連線也是一樣的。

二、同步post

// 1.根據**初始化oc字串物件

nsstring *urlstr = [nsstring stringwithformat:@"%@", kvideourl];

// 2.建立nsurl物件

nsurl *url = [nsurl urlwithstring:urlstr];

// 3.建立請求

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];

// 4.建立引數字串物件

// 5.將字串轉為nsdata物件

nsdata *pramdata = [parmstr datausingencoding:nsutf8stringencoding];

// 6.設定請求體

// 7.設定請求方式

// 建立同步鏈結

nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];

三、非同步get

nsstring *urlstr = [nsstring stringwithformat:@""];

nsstring *newstr = [urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

nsurl *url = [nsurl urlwithstring:newstr];

nsurlrequest *requst = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:10];

//非同步鏈結(形式1,較少用)

[nsurlconnection sendasynchronousrequest:requst queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) ];

四、非同步post

// post請求

nsstring *urlstring = [nsstring stringwithformat:@"%@",kvideourl];

//建立url物件

nsurl *url = [nsurl urlwithstring:urlstring];

//建立請求

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:10];

//建立引數字串物件

//將字串轉換為nsdata物件

//建立非同步連線(形式二)

[nsurlconnection connectionwithrequest:request delegate:self];

一般的,當建立非同步連線時, 很少用到第一種方式,經常使用的是**方法。關於nsurlconnectiondatadelegate,我們經常使用的協議方法為一下幾個:

// 伺服器接收到請求時

- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response

// 當收到伺服器返回的資料時觸發, 返回的可能是資源片段

- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data

// 當伺服器返回所有資料時觸發, 資料返回完畢

- (void)connectiondidfinishloading:(nsurlconnection *)connection

// 請求資料失敗時觸發

- (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error

最後,分析一下這幾種呢網路請求的區別。

get請求和post請求的區別:

1. get請求的介面會包含引數部分,引數會作為**的一部分,伺服器位址與引數之間通過? 來間隔. post請求會將伺服器位址與引數分開,請求介面中只有伺服器位址,而引數會作為請求的一部分,提交後台伺服器

2. get請求引數會出現在介面中,不安全.而post請求相對安全

3.雖然get請求和post請求都可以用來請求和提交資料,但是一般的get多用於從後台請求資料, post多用於向後台提交資料

同步和非同步的區別:

同步鏈結:主線程去請求資料,當資料請求完畢之前,其他執行緒一律不響應,會造成程式就假死現象

非同步鏈結:會單獨開乙個執行緒去處理網路請求,主線程依然處於可互動狀態,程式執行流暢

ios中的網路請求

get請求 請求的位址 對於中文的話要對請求位址實行 utf 8編碼 urlstring stringbyaddingpercentescapesusingencoding nsutf8stringencoding nsurlrequest request nsurlrequest requestw...

iOS的網路請求

首先建立乙個 uiviewcontroller,然後在.m檔案中寫入 簽訂協議 inte ce mainviewcontroller 可變的資料屬性,用來拼接每一小塊資料 property nonatomic,retain nsmutabledata data property nonatomic,...

iOS 網路請求

pragma mark 網路請求 方式 非同步 ibaction delegatebuttondidclicked uibutton sender 方法 客戶端收到伺服器的響應 pragma mark 客戶端收到伺服器的響應 void connection nsurlconnection conne...