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