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