
HTTP簡(jiǎn)介:
HTTP(Hypertext transfer protocol),先說(shuō)下著幾個(gè)單詞,Hypertext是超文本(除了HTML外,也可以是帶有超鏈接的XML或JSON),protocol是協(xié)議,transfer翻譯應(yīng)該是移交(也可以翻譯成傳輸,運(yùn)輸,還有一個(gè)更具體的詞是transport),最開(kāi)始學(xué)校學(xué)習(xí)Http的所有市面能見(jiàn)到的書(shū)籍都翻譯成超文本傳輸協(xié)議,Http設(shè)計(jì)的本身是為了移交和操作資源
Http提供了幾種方法訪(fǎng)問(wèn)資源.
Http協(xié)議中定義了與服務(wù)器之間交互的八個(gè)方法:get(獲取資源),post(向指定資源提交數(shù)據(jù)進(jìn)行處理請(qǐng)求,增加或者更新資源),put(向指定資源位置上傳其最新內(nèi)容 ),delete(請(qǐng)求服務(wù)器刪除Request-URI所標(biāo)識(shí)的資源);
GET和POST異同點(diǎn)
1.可見(jiàn)性,get訪(fǎng)問(wèn)時(shí)URL是所有人都可見(jiàn)的,post則是不可見(jiàn)的.
2.長(zhǎng)度限制
get和post傳遞長(zhǎng)度都是是沒(méi)有限制的
之所以有人說(shuō)get的長(zhǎng)度是有限制的,是因?yàn)間et是放在URL中一起去請(qǐng)求的,瀏覽器限制了URL的長(zhǎng)度導(dǎo)致GET的參數(shù)不能太長(zhǎng),大概長(zhǎng)讀是1 KB;其實(shí)HTTP協(xié)議對(duì)URL的長(zhǎng)度進(jìn)行限制
POST是請(qǐng)求大小是沒(méi)有限制的,Web服務(wù)器會(huì)對(duì)post請(qǐng)求進(jìn)行控制,postIIS請(qǐng)求限制為最多為28.6M,get請(qǐng)求IIS限制為2KB,如有需要可根據(jù)需求修改長(zhǎng)度大小
3.安全性,與 post相比,get的安全性較差,因?yàn)樗l(fā)送的數(shù)據(jù)是 URL 的一部分(登錄的時(shí)候提交數(shù)據(jù)應(yīng)該沒(méi)人會(huì)用get),post比get更安全,因?yàn)閰?shù)不會(huì)被保存在瀏覽器歷史或 web 服務(wù)器日志中。
4.數(shù)據(jù)類(lèi)型,get只允許 ASCII 字符; post傳遞沒(méi)有限制,也允許二進(jìn)制數(shù)據(jù)。
5 . 編碼類(lèi)型, form的enctype屬性為表單控件的內(nèi)容編碼方式,常見(jiàn)有兩種: application/x-www-form-urlencoded(默認(rèn))和multipart/form-data, 不管get還是post都可以使用者兩種編碼方式,當(dāng)表單中含有type=file文件控件的時(shí)候發(fā)送方式只能使用post,內(nèi)容編碼方式只能使用multipart/part/form-data.
6 . 書(shū)簽, 緩存 , get請(qǐng)求可以收藏為書(shū)簽,能被緩存, post不可以被收藏為書(shū)簽,不可以被緩存
7 . 取值方式 , ASP.NET中對(duì)于get請(qǐng)求Request.QueryString取值, 對(duì)Post請(qǐng)求使用Request.Form
我們?cè)诖a中是如何實(shí)現(xiàn)的呢?
#define BASE_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
//把上面的數(shù)字換成兩部分 以 ?為分界線(xiàn)
#define URL_POST1 @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define URL_POST2 @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
GET同步 <注意這里使用了MJExtension字典轉(zhuǎn)模型框架>
MJExtension:http://www.itdecent.cn/p/bbe7c88d5756
#pragma mark ----------------GET同步--------------
-(IBAction)getTong:(UIButton *)sender{
// 1 創(chuàng)建URL對(duì)象
NSURL *url = [NSURL URLWithString:BASE_URL];
// 2 創(chuàng)建請(qǐng)求對(duì)象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1 創(chuàng)建請(qǐng)求方式 (默認(rèn)是get這一步可以不寫(xiě))
[request setHTTPMethod:@"get"];
// 3 創(chuàng)建響應(yīng)對(duì)象(有時(shí)會(huì)出錯(cuò))
NSURLResponse *response= nil;
// 4 創(chuàng)建連接對(duì)象(同步)
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// http://www.itdecent.cn/p/bbe7c88d5756
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSNumber *lastUpdateTime = dict[@"lastUpdateTime"];
NSLog(@"lastUpdateTime = %@",lastUpdateTime);
NSArray *Array = dict[@"news"];
self.dataArray = [NewsModel mj_objectArrayWithKeyValuesArray:Array];
for (NewsModel *newsModal in self.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
}
POST同步
#pragma mark ---------------POST同步-----------------
- (IBAction)postAction1:(UIButton *)sender {
NSLog(@"Post同步");
//1 創(chuàng)建URL對(duì)象
NSURL *url =[NSURL URLWithString:URL_POST1];
//2 創(chuàng)建請(qǐng)求對(duì)象
NSMutableURLRequest *resuest =[NSMutableURLRequest requestWithURL:url];
//2.1 創(chuàng)建請(qǐng)求方式
[resuest setHTTPMethod:@"post"];//get可以省略 但是post必須要寫(xiě)
//3 設(shè)置請(qǐng)求參數(shù)
NSData *tempData = [URL_POST2 dataUsingEncoding:NSUTF8StringEncoding];
[resuest setHTTPBody:tempData];//設(shè)置請(qǐng)求主體 外界看不見(jiàn)數(shù)據(jù)
//4 創(chuàng)建響應(yīng)對(duì)象
NSURLResponse *response = nil;
//5 創(chuàng)建連接對(duì)象
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:resuest returningResponse:&response error:&error];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSNumber *lastUpdateTime = dict[@"lastUpdateTime"];
NSLog(@"lastUpdateTime = %@",lastUpdateTime);
NSArray *Array = dict[@"news"];
self.dataArray = [NewsModel mj_objectArrayWithKeyValuesArray:Array];
for (NewsModel *newsModal in self.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
}
GET connection異步
#pragma mark ---------------GET connection異步-----------------
-(IBAction)getYibu:(UIButton *)sender{
NSURL *url = [NSURL URLWithString:BASE_URL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:10];
//連接服務(wù)器
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
//NSURLConnectionDataDelegate代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"didReceiveResponse");
_tempData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;{
NSLog(@"didReceiveData");
[_tempData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;{
_dataArray = [NSMutableArray arrayWithCapacity:1];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_tempData options:NSJSONReadingAllowFragments error:nil];
NSNumber *lastUpdateTime = dict[@"lastUpdateTime"];
NSLog(@"lastUpdateTime = %@",lastUpdateTime);
NSArray *Array = dict[@"news"];
self.dataArray = [NewsModel mj_objectArrayWithKeyValuesArray:Array];
for (NewsModel *newsModal in self.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}
GET Session異步
#pragma mark --------------GET Session異步請(qǐng)求-----------------
- (IBAction)getSession:(UIButton *)sender {
NSURL *url =[NSURL URLWithString:BASE_URL];
//創(chuàng)建session對(duì)象
NSURLSession *session =[NSURLSession sharedSession];
__weak typeof(self)temp =self;
//創(chuàng)建task(該方法內(nèi)部默認(rèn)使用get)直接進(jìn)行傳遞url即可
NSURLSessionDataTask *dataTask =[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
temp.dataArray = [NSMutableArray arrayWithCapacity:1];
//數(shù)據(jù)操作
NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSNumber *lastUpdateTime = dict[@"lastUpdateTime"];
NSLog(@"lastUpdateTime = %@",lastUpdateTime);
NSArray *Array = dict[@"news"];
self.dataArray = [NewsModel mj_objectArrayWithKeyValuesArray:Array];
for (NewsModel *newsModal in self.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
NSLog(@"idStr = %@",self.dataArray[1].type);
}];
//數(shù)據(jù)操作
[dataTask resume];
// 注意數(shù)據(jù)操作之后無(wú)法獲得數(shù)據(jù)
for (NewsModel *newsModal in self.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
}
POST Session異步請(qǐng)求
#pragma mark -------POST Session異步請(qǐng)求---------
- (IBAction)postSession:(UIButton *)sender {
//創(chuàng)建URL對(duì)象
NSURL *url =[NSURL URLWithString:URL_POST1];
//創(chuàng)建請(qǐng)求對(duì)象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"post"];
NSData *tempdata = [URL_POST2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempdata];
// 3 建立會(huì)話(huà) session支持三種類(lèi)型的任務(wù)
// NSURLSessionDataTask //加載數(shù)據(jù)
// NSURLSessionDownloadTask //下載
// NSURLSessionUploadTask //上傳
NSURLSession *session =[NSURLSession sharedSession];
// NSLog(@"%d",[[NSThread currentThread] isMainThread]);
__weak typeof(self)weakSelf = self;
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//解析
_dataArray = [NSMutableArray arrayWithCapacity:1];
NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSNumber *lastUpdateTime = dict[@"lastUpdateTime"];
NSLog(@"lastUpdateTime = %@",lastUpdateTime);
NSArray *Array = dict[@"news"];
self.dataArray = [NewsModel mj_objectArrayWithKeyValuesArray:Array];
for (NewsModel *newsModal in weakSelf.dataArray) {
NSLog(@"newsModal.title = %@",newsModal.title);
}
NSLog(@"當(dāng)前的線(xiàn)程為%@",[NSThread currentThread]);
//回到主線(xiàn)程 刷新數(shù)據(jù) 要是刷新就在這里面
dispatch_async(dispatch_get_main_queue(), ^{
for (NewsModel *model in weakSelf.dataArray) {
NSLog(@"summary = %@",model.summary);
}
});
}];
//啟動(dòng)任務(wù)
[dataTask resume];
}