https://www.baidu.com/login.php?name = 张三&password=123456 注:只是个?,不是真实URL
URL:地址分三部分
1.协议:http:// http协议 https协议 tcp/ip协议 UDP协议 XMPP协议等
http协议和https协议 一般用来请求网络数据,比如登录,注册,页面数据等,短链接协议
TCP/ip协议 属于长协议,一般用来实时通信,聊天等功能
XMPP 基于点对点的xml格式数据的通信协议
UDP 属于长链接,可用于游戏应用里的通信协议
2.ww.lanou3g.com 属于主机域名
根据域名去确定你访问的是哪个服务器 每个公司都有唯一的一个域名
3.路径 login.php?name = 张三&password=123456
通过路径访问指定的接口的数据
name和password 叫参数
GET请求和POST请求区别
1.get请求是将所有参数直接放到URL地址上,使用简单,但用户的隐私都会暴露在外面
2.post请求是将一部分或者全部参数放在以body体里,转成data形式数据来发送给服务器,外界不易截取,安全性高
同步与异步区别
1.同步是单线程,执行任务以主线程为主,一个任务执行完之后再执行下一个任务
2.异步是多线程,在主线程的基础上又开辟了一个子线程去帮助主线程完成任务,一般在子线程上完成的任务都是数据量比较大,比较耗时耗资源的任务
3.如果你是用子线程去请求网络数据,那么当在子线程上请求完数据之后,必须要回到主线程进行刷新页面的操作,否则子线程不会给你刷新数据
另外子线程的任务一般慢于主线程,通常主线程用于布局页面,子线程用于请求数据,所以页面先布局,后才将数据请求完,如果不重新刷新页面,导致数据不会显示
// NSURLConnection iOS9之后就被废弃了
// DEPRECATED deprecated废弃的意思
// NSURLSession 已经代替NSURLConnection功能上差不多,NSURLSession使用起来更方便,支持下载和上传文件,断点续传等使用起来很方便
//苹果将每一次请求都定义为一个任务
// NSURLSessionDataTask 是请求普通网络数据的任务类
// NSURLSessionDownloadTask 下载任务类
// DEPRECATED deprecated废弃的意思
// NSURLSession 已经代替NSURLConnection功能上差不多,NSURLSession使用起来更方便,支持下载和上传文件,断点续传等使用起来很方便
//苹果将每一次请求都定义为一个任务
// NSURLSessionDataTask 是请求普通网络数据的任务类
// NSURLSessionDownloadTask 下载任务类
// NSURLSessionUploadTask 上传任务类(需要和服务器配合使用)
get请求
- (IBAction)getAction:(id)sender {
//url字符串地址
NSString *urlStr = @" http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
//中文编码
NSString *strEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//废弃了 (只能处理中文,不能处理特殊字符)
//NSString *strEncode2 = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//生成系统能够识别的NSURL对象
NSURL *url = [NSURL URLWithString:strEncode];
//创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//创建网络连接对象
NSURLSession *session = [NSURLSession sharedSession];
//创建请求普通数据网络数据任务task
self.task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//json解析data数据
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result = %@", result);
}];
//开始请求任务
[self.task resume];
//url字符串地址
NSString *urlStr = @" http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
//中文编码
NSString *strEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//废弃了 (只能处理中文,不能处理特殊字符)
//NSString *strEncode2 = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//生成系统能够识别的NSURL对象
NSURL *url = [NSURL URLWithString:strEncode];
//创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//创建网络连接对象
NSURLSession *session = [NSURLSession sharedSession];
//创建请求普通数据网络数据任务task
self.task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//json解析data数据
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result = %@", result);
}];
//开始请求任务
[self.task resume];
}
post请求
- (IBAction)postButton:(id)sender {
//post地址
NSString *urlStr = @" http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=cee6710ae48a3945b398702d8702510a&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C552EE383-0FAD-4555-9979-AC38A01C5D6D%7C9C579DCC-7C8F-4E53-AEB6-54527C473309&format=json&loguid=&method=Recipe.getFindRecipe&nonce=1443856978&sessionid=1443856790&signmethod=md5×tamp=1443856978&uuid=02288be08f4b871a69565746255b0de9&v=2&vc=40&vn=v5.1.0";
NSString *strEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:strEncode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求对象的请求方式为POST,默认是GET请求
[request setHTTPMethod:@"POST"];
NSString *bodyStr = @"cacheKey=Recipe.getFindRecipe&sign=&uid=&uuid=02288be08f4b871a69565746255b0de9";
//将body体字符串转成data型数据
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//设置为请求的body体
[request setHTTPBody:bodyData];
//创建一个专门配置session的类,是系统对session对象的一个标准配置
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
//另外一个初始化方法
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
self.posttask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//json解析data数据
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"postresult = %@", result);
}];
[self.posttask resume];
}
//post地址
NSString *urlStr = @" http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=cee6710ae48a3945b398702d8702510a&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C552EE383-0FAD-4555-9979-AC38A01C5D6D%7C9C579DCC-7C8F-4E53-AEB6-54527C473309&format=json&loguid=&method=Recipe.getFindRecipe&nonce=1443856978&sessionid=1443856790&signmethod=md5×tamp=1443856978&uuid=02288be08f4b871a69565746255b0de9&v=2&vc=40&vn=v5.1.0";
NSString *strEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:strEncode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求对象的请求方式为POST,默认是GET请求
[request setHTTPMethod:@"POST"];
NSString *bodyStr = @"cacheKey=Recipe.getFindRecipe&sign=&uid=&uuid=02288be08f4b871a69565746255b0de9";
//将body体字符串转成data型数据
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//设置为请求的body体
[request setHTTPBody:bodyData];
//创建一个专门配置session的类,是系统对session对象的一个标准配置
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
//另外一个初始化方法
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
self.posttask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//json解析data数据
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"postresult = %@", result);
}];
[self.posttask resume];
}
}