http協(xié)議整個請求過程:
① 先建立TCP鏈接,三次握手
② 根據(jù)網(wǎng)址(NSURL:同一資源定位符,網(wǎng)址就是資源,我們所需要的數(shù)據(jù)在服務(wù)器端存儲的位置)向網(wǎng)址發(fā)送請求(NSURLRequest),請求一般包含請求頭(一般不用做改動),請求體(這里能看到的就是POST請求的時候需要給參數(shù))。請求一般用到兩種GET和POST。GET:一般情況下都是將參數(shù)拼接在網(wǎng)址后邊,但是不是將參數(shù)拼接在網(wǎng)址后邊的就是GET請求。GET請求一般能傳遞的數(shù)據(jù)大小為255字節(jié)。由于它是將參數(shù)拼接在網(wǎng)址后邊,其他人員可以看到該參數(shù),所以安全性較差。POST:在實際代碼中使用的是(NSMutableURLRequest),是將參數(shù)轉(zhuǎn)換為NSData類型,發(fā)送給服務(wù)器,一般不是直接拼接在網(wǎng)址后邊,它可以傳輸?shù)臄?shù)據(jù)量理論上是無限制的,安全性較好。
③ 發(fā)送請求,建立客戶端與服務(wù)器端的鏈接(NSURLConnection),連接的方式分為兩種:同步和異步,同步:當(dāng)建立同步連接的時候,該請求沒有返回數(shù)據(jù)的時候,那么其他操作都不能進行。如果實在代碼中,同步請求未結(jié)束,它底下的代碼不會執(zhí)行。異步:異步連接,在數(shù)據(jù)未返回的時候,我們可以進行其它操作,在代碼中的體現(xiàn)就是,發(fā)送了請求之后,即使數(shù)據(jù)未返回,它底下的代碼也可以執(zhí)行。異步的實現(xiàn)方式有兩種,一種是通過代理,一種是block回調(diào)。
④ 得到服務(wù)器的返回數(shù)據(jù)(NSURLResponse),返回也會包括響應(yīng)頭,響應(yīng)體(實際上所需要的數(shù)據(jù))。
⑤ 斷開TCP連接,四次分手。
GET同步請求
- (void)getAndSynchronousMethod
{
//定義URL網(wǎng)址
NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSURL *url = [NSURL URLWithString:urlString];
//初始化請求方式,默認為GET
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
// 創(chuàng)建同步連接,request:請求對象,里邊承載著我們的請求信息,有網(wǎng)址,請求頭等信息。response:請求的返回(響應(yīng)),里面包含了響應(yīng)頭的一些信息,如果需要響應(yīng)頭,需要傳遞此參數(shù),一般不需要。error:請求出錯的時候,會有錯誤信息保存在該參數(shù)中,一般置為nil,可以根據(jù)返回數(shù)據(jù)來判斷是否請求有問題
NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
[self jsonParserWithData:receiveData];
}
POST同步請求
- (void)postAndSynchronousMethod
{
NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
//date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213
NSURL *url = [NSURL URLWithString:urlString];
//創(chuàng)建POST請求參數(shù),為NSData類型
NSString *postString = @"date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
//將string類型轉(zhuǎn)換為NSData類型
NSData *postParameterData = [postString dataUsingEncoding:NSUTF8StringEncoding];
//創(chuàng)建請求,因為NSURLRequest類型不能設(shè)置請求方式,所以如果是post請求,就得使用它的子類NSMutableURLRequest
NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:url];
//設(shè)置請求方式
mutableReq.HTTPMethod = @"POST";
//設(shè)置請求參數(shù)
mutableReq.HTTPBody = postParameterData;
//建立同步連接
NSData *receiveData = [NSURLConnection sendSynchronousRequest:mutableReq returningResponse:nil error:nil];
[self jsonParserWithData:receiveData];
}
POST異步 實現(xiàn)方式block
- (void)postAndAsynchronousMethod
{
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSData *postData = [[NSString stringWithFormat:@"date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//設(shè)置請求方式
request.HTTPMethod = @"POST";
//設(shè)置請求參數(shù)
request.HTTPBody = postData;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
[self jsonParserWithData:data];
}];
NSLog(@"123456");
}
GET異步 實現(xiàn)方式block
- (void)getAndAsynchronousMethod
{
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//異步連接,block實現(xiàn)
//queue:需要將請求連接放到一個隊列中,目前,我們是將該請求放到主隊列中,在主隊列中操作所占有的資源的優(yōu)先等級高
//completionHandler:請求有返回結(jié)果時,會執(zhí)行該block回調(diào)
//block中的參數(shù):response:請求返回的響應(yīng),內(nèi)部包含響應(yīng)頭。data:是我們所需要的實際數(shù)據(jù)。connectionError:請求出錯時返回的錯誤信息
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
[self jsonParserWithData:data];
}];
NSLog(@"我實在異步block底下打印");
}