1.HTTP协议,Hyper Text Transfer Protocol(超⽂文本传输协议)是
⽤用于从万维⺴⽹网服务器传送超⽂文本到本地浏览器的传输协议,HTTP是 ⼀一个应⽤用层协议,由请求和响应构成,是⼀一个标准的客户端服务器模型。
2.BS和CS的区别以及优缺点 :
1)C/S又称Client/Server或客户/服务器模式。服务器通常采用高性能的PC、工作站或小型机,并采用大型数据库系统,如Oracle、Sybase、Informix或 SQL Server。客户端需要安装专用的客户端软件。
2)B/S是Brower/Server的缩写,客户机上只要安装一个浏览器(Browser),如Netscape Navigator或Internet Explorer,服务器安装Oracle、Sybase、Informix或 SQL Server等数据库。浏览器通过Web Server 同数据库进行数据交互。
3)优缺点
C/S的优点是能充分发挥客户端PC的处理能力,很多工作可以在客户端处理后再提交给服务器。对应的优点就是客户端响应速度快。缺点主要有以下几个:
只适用于局域网。 客户端需要安装专用的客户端软件。涉及到安装的工作量,安装或维护。还有软件维护和升级成本非常高。 对客户端的操作系统一般也会有限制。
B/S最大的优点就是可以在任何地方进行操作而不用安装任何专门的软件。只要有一台能上网的电脑就能使用,客户端零维护。系统的扩展非常容易。
3 .实现HTTP 协议请求
网络请求地址对象NSURL
网络请求对象NSURLRequest、NSMutableURLRequest
网络链接对象NSURLConnection
网络链接协议NSURLConnectionDelegate
网络请求数据信息NSURLResponse
4.url
(1).统⼀一资源定位符,也被称为⺴⽹网址
(2).url的符语法: 协议://授权/路径?查询 。
(3).协议:ftp://(⽂文件传输协议) http://(超⽂文本传输协议) https://(安全超文本传输协议) file://(本地文件协议)
(4)url作为网址字符串包含很多请求参数,NSURL对网址字符串进行封装, 可以使用NSURL对象获取相应的参数。
absoluteString: http://lily:123456@www.google.com/search? hl=en&source=hp&q=mysql&aq=f&oq=&aqi=g10#page
scheme: http
host: www.google.com
user: lily
password: 123456
port: 8080
query: hl=en&source=hp&q=mysql&aq=f&oq=&aqi=g10
5.网络请求方式 GET POST
比较:1.传输数据的方式:GET:通过网址字符串。POST:通过data。
2.传输数据的大小:GET :网址字符串最多255字节,POST:使用NSData ,容量超过1G。
3.安全性:GET:所有传输给服务器的数据,显示在网址里,类似于密码的明文输入,直接可见。
POST:数据被转换成NSData (二进制数据),类似于密码的密文输入,无法直接读取。
连接方式
同步连接:程序容易出现卡死现象。
异步连接:等待数据返回。异步连接两种方式 : 设置代理,接收数据; 实现block
//同步 :需要有返回值。NSData 接收
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
//异步 :不需要返回值
+ (void)sendAsynchronousRequest:(NSURLRequest*) request
queue:(NSOperationQueue*) queue
completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler NS_AVAILABLE(10_7, 5_0);
整体思路:
获得NSData 数据对象。使用JSON 方式解析数据。
1.获取NSData的方式
同步:方法返回值就是NSData 数据对象。
异步:代理方式:定义全局变量 NSData 类型的 allData, 使用 [_allData appendData:data];拼接data。
block块方式:block 内部就有data 数据对象,但是只能在内部使用。 所以在block 内部解析data 数据对象。因为block 的特殊执行顺序。需要在block 内部解析完数据 后重新加载页面。
2.POST 与GET实现 的不同之处:
// 1)POST 需要 创建NSMutableURLRequest 请求对象 (NSMutableURLRequest 与NSURLRequest 的区别:NSMutableURLRequest可以设置 HTTPMethod等属性,NSURLRequest不可以。请求方式默认是GET)
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
// 2) 设置请求方式
[request setHTTPMethod:@"POST"];
// 3) 创建 BOOY 对象
NSData *bodyData = [POST_BOOY dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
//其他都相同
GET 同步请求代码
#pragma mark - GET 同步请求 (重点在于获得同步请求的NSData 对象 )
@property (nonatomic, strong) NSMutableArray * dataArray;
- (IBAction)getSynchronousAction:(id)sender
{
// 1.创建URL
NSURL * url = [[NSURL alloc] initWithString:GET_URL];
// 2.创建请求对象 (可变的可以设置请求方式)
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
// 设置请求方式,默认是GET
[request setHTTPMethod:@"GET"];
// 3.创建响应对象 预创建两个指针,先是空的,(可以不设置,直接写nil)
NSURLResponse * response = nil;
// 4.创建错误对象(可以不设置,直接写nil)
NSError * error = nil;
// 5. 链接请求数据,获取data对象
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error:&error];
/* --------------------------数据处理(使用JSON 系统提供的方式)------------------------*/
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_dataArray = [[NSMutableArray alloc] initWithCapacity:30];
NSArray * array = [dic objectForKey:@"news"];
for (NSDictionary * dic in array) {
News * news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//验证
for (News * news in _dataArray) {
NSLog(@"%@",news);
}
}
GET 异步请求delegate 方式代码
#pragma mark - GET 异步请求 -Delegate
//数据一块一块的过来,需要拼接。data 拼接对象
@property (nonatomic,strong) NSMutableData * allData;
- (IBAction)getAsynchronousActionWithDelegate:(id)sender
{
// 1. 创建URL
NSURL * url = [[NSURL alloc] initWithString:GET_URL];
// 2.创建请求对象
NSURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
// 3. 创建连接对象,并设置代理
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 开始请求
[connection start];
}
#pragma mark - 实现代理方法
#pragma mark - 1.收到响应对象
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 收到服务器响应后开始准备数据
_dataArray = [[NSMutableArray alloc] initWithCapacity:30];
_allData = [[NSMutableData alloc] initWithCapacity:30];
}
#pragma mark -2.收到数据对象
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 多次执行,数据拼接
[_allData appendData:data];
}
#pragma mark -3.完成数据请求
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 解析数据
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:_allData options:NSJSONReadingAllowFragments error:nil];
NSArray * array = [dic objectForKey:@"news"];
// 封装model 对象
for (NSDictionary * dic in array) {
News *news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
// 校验
for (News * news in _dataArray) {
NSLog(@"%@",news);
}
}
#pragma mark -4. 错误处理
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"出错了,错误代码为%@",error);
}
GET 异步请求Block 方式
#pragma mark - GET 异步响应 Block
- (IBAction)getAsynchronous:(id)sender
{
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:GET_URL]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 解析数据
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_dataArray = [[NSMutableArray alloc] initWithCapacity:30];
// 封装model
for (NSDictionary * dic in dict[@"news"]) {
News * news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
// 验证
for (News * news in _dataArray) {
NSLog(@"%@",news);
}
//输出response
NSLog(@"response = %@",response);
NSLog(@"%@",connectionError); // 没有error 输出是nil
// 异步不影响主线程UI 绘制,另开一个线程进行数据接受处理。
先执行block 块外面的,再执行block里面的。所以需要在block内部实现数据刷新。
[self text:_dataArray];
//tableView 有自己刷新数据的方法 [self.tableView reloadData];
}];
}
-(void)text:(NSArray *)array
{
// 视图处理。 设置数据。
// 默认数据,程序执行,数据加载。 显示默认。
// 执行上面方法时,因为重新调用, 赋值给新的数据,实现数据显示,并刷新。
}
POST 同步请求
#define POST_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"
#define POST_BOOY @"date=20150921&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"
#pragma mark - POST 同步请求
- (IBAction)postSasynchronousAction:(id)sender
{
// 创建URL
NSURL *url = [[NSURL alloc] initWithString:POST_URL];
// 创建请求对象
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
// 设置请求方式
[request setHTTPMethod:@"POST"];
// 创建 BOOY 对象
NSData *bodyData = [POST_BOOY dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
// 链接请求数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
/*----------------数据解析--------------*/
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
_dataArray = [[NSMutableArray alloc] initWithCapacity:20];
NSArray * array = [dic objectForKey:@"news"];
for (NSDictionary * dic in array) {
News * news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
for (News * news in _dataArray) {
NSLog(@"%@",news);
}
}
POST 异步请求 block 方式
- (IBAction)postAsynchronousWithBlockAction:(id)sender
{
// 创建URL
NSURL * url = [[NSURL alloc] initWithString:POST_URL];
// 创建请求对象
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"post"];
// 创建dataBooy
NSData * bodyData = [POST_BOOY dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
// 异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 解析数据
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray * array = [dic objectForKey:@"news"];
//封装
_dataArray = [[NSMutableArray alloc] initWithCapacity:30];
for (NSDictionary *dic in array) {
News * news = [News new];
[news setValuesForKeysWithDictionary:dic];
[_dataArray addObject:news];
}
//校验
for (News * news in _dataArray) {
NSLog(@"%@",news);
}
}];
}