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];
}];
}
网友评论