标题
http:
超文本传输协议(HTTP, HyperText Transfer Potocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。
http是用于www(万维网)浏览传输数据的一个协议
http://服务器地址 资源的地址
www是环球信息网的缩写,(亦作”web”)
IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层(识别数据内容)
http协议的作用:
(1)规定客户端和服务器端之间的数据传输格式
(2)让客户端和服务器能有效地进行数据沟通
为什么选择使用HTTP
(1) 简单快速 因为HTTP协议简单,所以HTTP服务器的程序规模小,因而通信速度很快
(2) 灵活 HTTP允许传输任意类型的数据
(3) 是飞持续连接 限制每次连接只处理一个请求,服务器对客户端的请求做出响应后,马上断开连接,这种方式节省时间
HTTP的通信过程:
(1)请求:客户端向服务器索要数据
(2)响应:服务器返回客户端相应的数据
*********
HTTP的请求方式:get post
get 会把请求的内容 拼接到 链接 地址 里面(数据请求时的时候 默认是get)
www.baidu.com/user/login?userName = @“wo”,psw = 123
get的特征:
1、浏览器和服务器对URL长度有限制,因此在URL后面附带的参数是有限制的,通常不能超过1KB
(2)、会把请求的数据 暴露在接口里面
(3)、如果仅仅是索取数据(数据查询),建议使用
POST的特征
2.post:参数全部放在请求体中
(1)这样就保证了数据的安全
(2)没有具体的长度限制(唯一的限制就是服务器的承受能力)
3.选择GET POST的建议
(1) 如果要传递大量的数据,比如上传文件,只能用POST请求
(2) GET的安全性比POST差些,如果包含机密、敏感信息,建议用POST
(3) 如果仅仅是索要数据(数据查询),建议使用GET
(4) 如果是增加、修改、删除数据,建议使用POST
接口写法:
URL(统一资源定位符)
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadData1];
// [self loadData3];
[self loadData5];
}
通过URL 获得 URL 里面的内容(字符串)
- (void)loadData1
{
把字符串 转成 NSURL
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
}
通过URL 获得到 URL里面的data
- (void)loadData2
{
NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithData:data];
imageView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imageView];
}
网络请求有:同步请求异步请求
同步请求:等所有操作完全执行完毕 才会继续执行
同步请求的 弊端:会遇到假死的情况(只要请求的操作没有 执行完毕就不会再去响应 任何事件(在同一线程))
异步请求:在程序运行的时候 会利用空闲的时间 去执行里面的操作不会影响到 同一线程里面的其他操作
同步请求
- (void)loadData3
{
NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];
实例化请求对象 里面携带着 请求的地址
NSURLRequest *request = [NSURLRequest requestWithURL:url];
data 服务器响应给咱们的数据
NSURLConnection 发送请求的类
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithData:data];
imageView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imageView];
}
异步请求
- (void)loadData4
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imageView];
NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];
实例化请求对象 里面携带着 请求的地址
NSURLRequest *request = [NSURLRequest requestWithURL:url];
需要通过 连接异步发送 的请求
线程
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
发送一个异步请求在queue 这个线程里面执行
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
respond 服务器 回应的 内容(回应状态的code以及error)
data 回应给 客户端 需要的数据
NSLog(@"%@",response);
imageView.image = [UIImage imageWithData:data];
}];
}
get 把传输的数据放在链接地址里面
- (void)loadData5
{
NSString *interfaceString = @"http://apis.baidu.com/showapi_open_bus/mobile/find";
NSString *requestContentString = @"num=15108593301";
NSString *urlString = [NSString stringWithFormat:@"%@?%@",interfaceString,requestContentString];
把连接地址字符串转成 NSUTF8StringEncoding
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
可变请求可以添加 请求方式以及请求的 请求头或则更多
timeoutInterval 请求所需的时间超过时间 不再发送这个请求
cachePolicy 缓存内容的方式
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
指定http的请求方式用GET
request.HTTPMethod = @"GET";
NSString *apiKey = @"e7f5ac9e7c42a6c8cb125ee1d7e8779e";
把apikey 发送给 服务器 指定的请求头 位置
forHTTPHeaderField 需要的KEY是服务器指定的key
[request addValue:apiKey forHTTPHeaderField:@"apikey"];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",response);
解析 json 文件
把data 转换成json 文件
NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error:nil];
NSLog(@"%@ %@",info[@"showapi_res_body"][@"city"],info[@"showapi_res_body"][@"name"]);
}];
}
- (void)loadData6
{
NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetService"];
请求的参数
PlatformType 设备类型 3表示ios设备
NSDictionary *dic = @{@"PlatformType":@"3"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];