URL
URL
全稱是 Uniform Resource Locator(統(tǒng)一資源定位符).也就是說,通過一個URL
, 能找到互聯(lián)網(wǎng)上唯一的1個資源
-
URL
就是資源的地址\位置,互聯(lián)網(wǎng)上的每一個資源都有一個唯一的URL
. -
URL
的基本格式 = 協(xié)議://主機地址/路徑 - http://www.lanou3g.com/szzr/
- 協(xié)議:不同的協(xié)議,代表值不同的資源查找方式,資源傳輸方式
- 主機地址:存放資源的主機的 IP 地址(域名)
- 路徑:資源在主機中的位置.
HTTP協(xié)議
- 所謂的協(xié)議;就是用于萬維網(wǎng)(WWW)服務器傳送超文本到本地瀏覽器的傳輸協(xié)議.
網(wǎng)絡請求方式
- GET
- POST
相同點 都能給服務器傳輸數(shù)據(jù)
不同點 1,給服務器傳輸數(shù)據(jù)的方式不同:GET
通過網(wǎng)址字符串.POST
通過 Data
2,傳輸數(shù)據(jù)的大小不同:GET
網(wǎng)址字符串最多為255字節(jié).POST
使用 NSData, 容量超過1G.
3,安全性:GET
所有傳輸給服務器的數(shù)據(jù),顯示在網(wǎng)址里,類似于密碼的明文輸入,直接可見.POST
數(shù)據(jù)被轉成 NSData(二進制數(shù)據(jù)).類似于密碼的密文輸入.無法直接讀取.
HTTP 協(xié)議請求如何實現(xiàn)的
- 網(wǎng)絡請求地址對象 NSURL 的作用及用法
- 網(wǎng)絡請求對象 NSURLRequest,NSMutableURLRequest 的作用及用法
- 網(wǎng)絡連接對象 NSURLConnection 的作用及用法
- 網(wǎng)絡廉潔協(xié)議 NSURLConnectionDelegate 的作用及用法
GET同步鏈接
- (void)getAndSychronous{
//1,創(chuàng)建網(wǎng)址對象,在連接中不允許有空格出現(xiàn)
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];
//2,創(chuàng)建請求對象
NSURLRequest *request = [NSURLrequest requestWithURL:url];
//3,發(fā)送請求,連接服務器
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//4,解析
if(data){
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSData *string1 = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:string1 encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
}
- (void)viewDidLoad{
[self getAndSychronous];//調用
}
GET 異步鏈接(block 鏈接)
- (void)getAndSychronousBlock{
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"];
//創(chuàng)建請求體
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//response..響應頭的信息.data 我們所需要的真實的數(shù)據(jù), connectionError:連接服務器錯誤信息.
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"請求數(shù)據(jù)");
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}];
}