POST和GET請求數據的流程

GET請求與POST請求區別

1.GET請求的接口會包含參數部分,參數會作為網址的一部分,服務器地址與參數之間通過?來間隔.POST請求會將服務器地址與參數分開,請求接口中只有服務器地址,而參數會作為請求體的一部分,提交給后臺服務器?

2.GET請求參數會出現在接口中,不安全,而POST請求相對安全?

3.雖然GET請求與POST請求都可以用來請求與提交數據,POST多用于向后臺提交數據,GET多用于從后臺請求數據?

4.同步與異步的區別: 同步連接:主線程去請求數據,當數據請求完畢之前,其它操作一律不響應,會造成假死現象 異步連接:會單獨開一個線程去處理網絡請求,主線程依然處于交互,程序運行流暢

#define kVideoURL @"http://api.tudou.com/v3/gw”

一. 異步POST請求方式

1.根據網址初始化OC字符串對象?

NSString *urlString = [NSString stringWithFormat:@"%@",kVideoURL];?

2.創建NSURL對象?

NSURL *url = [NSURL URLWithString:urlString];?

3.創建請求?

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];?

4.創建參數字符串對象

?NSString *parmStr = [NSString stringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];?

5.將字符串轉為NSData對象?

NSData *parmData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];

?6.設置請求體?

[request setHTTPBody:parmData];?

7.設置請求方式(POST)

?[request setHTTPMethod:@"POST”];

8.異步連接(協議代理)?

[NSURLConnection connectionWithRequest:request delegate:self];

9.遵從<NSURLConnectionDataDelegate>協議

10.實現協議中方法

當服務器響應時觸發

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

self.data = [NSMutableData data];

//當服務器響應時,為data開辟空間,接下來服務器返回數據

}

當接收服務器返回的數據時觸發,返回的可能是資源片段

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

拼接數據

[self.data appendData:data];

}

當服務器返回所有數據時觸發,數據返回完畢

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

self.data;//就是服務器返回的所有數據

解析,獲得請求數據

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options: NSJSONReadingMutableContainers error:nil];

}


二.異步GET請求方式

1.將網址初始化成OC字符串對象

NSString *urlString = [NSString stringWithFormat:@"http://image.zcool.com.cn/56/13/1308200901454.jpg”];

2.如果網址中存在中文,進行URLEncode(沒有則不需要)

NSString *newUrl = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

3.構建網絡URL對,NSURL

NSURL *url = [NSURL URLWithString:newUrl];

4.創建網絡請求

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

5.異步連接(協議代理)

[NSURLConnection connectionWithRequest:request delegate:self];

6.服從協議,并且實現協議中的方法(和異步POST一樣)


三.同步GET請求方式

1.將網址初始化成OC字符串對象

NSString *urlString = [NSString stringWithFormat:@"%@?query=%@?ion=%@&output=json&ak=6E823f587c95f0148c19993539b99295",kBusinessInfoURL,@"銀行",@"濟南"];

2.如果網址中存在中文,進行URLEncode

NSString *newUrl = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

3.構建網絡URL對,NSURL

NSURL *url = [NSURL URLWithString:newUrl];

4.創建網絡請求

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

5.同步連接

NSURLResponse *response = nil;

NSError *error = nil;

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

6.解析,獲取請求到的數據

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


四. 同步POST請求方式

1.根據網址初始化OC字符串對象

NSString *urlString = [NSString stringWithFormat:@"%@",kVideoURL];

2.創建NSURL對象

NSURL *url = [NSURL URLWithString:urlString];

3.創建請求?

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

4.創建參數字符串對象?

NSString *parmStr = [NSString stringWithFormat:@"method=album.channel.get&appKey=myKey&format=json&channel=t&pageNo=1&pageSize=10"];

5.將字符串轉為NSData對象

NSData *parmData = [parmStr dataUsingEncoding:NSUTF8StringEncoding];

6.設置請求體

[request setHTTPBody:parmData];

7.設置請求方式(POST)

[request setHTTPMethod:@"POST"];

8.同步連接

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

9.解析,獲得請求數據

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容