ios9之后网络编程的改变:
(1)ios之后弃用NSURLConnection
NSURLSession:ios7.0之后出来,ios9只能使用这个类
(2)Xcode7.0在ios9之后,后台服务器传输协议由HTTP改成HTTPS,因此对于HTTP的URL使用,就可以降低Xcode网络链接的安全性(方法是: iOS开发需要在info.plist中添加一对key值即 App Transport Security Settings 中把Allow Arbitrary Loads 改为YES)
如果想用弃用的类:需要调整 该应用程序 最低支持的系统版本
一、URL:
1、概念:Uniform Resource Locator 统一资源定位符
2、结构:URL包含 模式(或称协议)、服务器名称(或IP地址)、路径和文件名
例如
可以通过URL找到服务器中的文件
二、HTTP 超文本传输协议 (HTTPS 是HTTP的安全版)
通讯流程:首先客户端发送一个请求(request)给服务器,服务器在接收到这个请求后会有一个响应(response)返回给客户端
【【如果发送一个请求,没有任何响应:
(1)网络可能出现问题
(2)请求内容错误:URL
【【如果发送了一个请求,有回应但是没能得到请丢掉内容(这时候一般情况下在回应中有错误代码code:XXX)
(1)服务器需要的内容 与 发送请求内容不匹配
(2)服务器需要的内容 发送请求的时候未发送
(3)后台服务器代码问题
常用请求方法
( 1 ) get 数据写下 URL 后面
( 1 ) get 数据写下 URL 后面
(2)post 写在请求体中(单独有一个请求体) post传递的数据量没有限制(具体看服务器的处理能力)
三、request:
1、概念:是URL加载的请求;
类名:NSURLRequest
在请求中需要两个基本元素:(1)要加载的URL
(2)加载的两种方式:get(默认方式)、post
2、
NSURLRequest的子类是
NSMutableURLRequest,可以添加请求体、请求头
request有两种请求方法:
(1)不考虑缓存策略的两种方法
+ (instancetype)requestWithURL:(NSURL *)URL;
- (instancetype)initWithURL:(NSURL *)URL;
(2)考虑缓存策略 超时限制 的两种方法
(
1
)
+ (instancetype)requestWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
(2)- (instancetype)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
#pragma mark ---
简单的网络请求实例:请求一张网络图片
NSString *string = @"http://www.bz55.com/uploads/allimg/130803/1-130P3112Q0.jpg" ;
// 注意: URL 尽量不要出现中文字符,如果出现中文字符 就需要转码
// 转码的方法: NSString *string = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];<- 该方法已经不能用了
NSURL *url = [ NSURL URLWithString :string];
NSLog ( @"%@" ,url);
NSData *imageData = [ NSData dataWithContentsOfURL :url];
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame : self . view . frame ];
imageView. contentMode = UIViewContentModeScaleAspectFit ;
[ self . view addSubview :imageView];
imageView. image = [ UIImage imageWithData :imageData];
// 必须请求完成之后 才会去 做其它的事情(同步请求)
#pragma mark---request 示例 1 、不考虑缓存策略
// http://apis.baidu.com/apistore/weatherservice/citylist
/*
天气预报接口 :http://apis.baidu.com/apistore/weatherservice/citylist
请求方式: get
需要传的参数:( 1 ) header:apikey
(2) 内容: cityname
*/
// http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou ? 前面是 URL , ? 后面是要发送的内容
NSString *string1 = @"http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou" ;
NSURL *url1 = [ NSURL URLWithString :string1];
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url1];
// 请求方式 如果不写 默认是 GET
request. HTTPMethod = @"GET" ;
// 当需要请求头时 需要使用可变的 request
[request addValue : @"c2e76db5c772e8fb78b6f4731de39cd1" forHTTPHeaderField : @"apikey" ];
#pragma mark---request 示例 2 、考虑缓存策略 超时限制
/*
cachePolicy:
NSURLRequestUseProtocolCachePolicy , 默认的缓存策略 如果缓存不存在 直接从服务端获取 如果缓存存在 会根据 response 中的 Cache-Control 字段判断下一步操作
NSURLRequestReloadIgnoringCacheData 忽略缓存 重新请求
NSURLRequestReturnCacheDataElseLoad 有缓存就使用 不管其有效性 没有缓存就重新请求
NSURLRequestReturnCacheDataDontLoad 有缓存就用缓存 没有缓存也不发请求 当做请求出错处理(用于离线模式)
timeoutInterval :超时的时间间隔 单位:秒
*/
NSString *urlString = @"http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou" ;
NSMutableURLRequest *request1 = [ NSMutableURLRequest requestWithURL :[ NSURL URLWithString :urlString] cachePolicy : NSURLRequestReloadIgnoringCacheData timeoutInterval : 15 ];
request1. HTTPMethod = @"GET" ;
[request1 addValue : @"c2e76db5c772e8fb78b6f4731de39cd1" forHTTPHeaderField : @"apikey" ];
NSString *string = @"http://www.bz55.com/uploads/allimg/130803/1-130P3112Q0.jpg" ;
// 注意: URL 尽量不要出现中文字符,如果出现中文字符 就需要转码
// 转码的方法: NSString *string = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];<- 该方法已经不能用了
NSURL *url = [ NSURL URLWithString :string];
NSLog ( @"%@" ,url);
NSData *imageData = [ NSData dataWithContentsOfURL :url];
UIImageView *imageView = [[ UIImageView alloc ] initWithFrame : self . view . frame ];
imageView. contentMode = UIViewContentModeScaleAspectFit ;
[ self . view addSubview :imageView];
imageView. image = [ UIImage imageWithData :imageData];
// 必须请求完成之后 才会去 做其它的事情(同步请求)
#pragma mark---request 示例 1 、不考虑缓存策略
// http://apis.baidu.com/apistore/weatherservice/citylist
/*
天气预报接口 :http://apis.baidu.com/apistore/weatherservice/citylist
请求方式: get
需要传的参数:( 1 ) header:apikey
(2) 内容: cityname
*/
// http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou ? 前面是 URL , ? 后面是要发送的内容
NSString *string1 = @"http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou" ;
NSURL *url1 = [ NSURL URLWithString :string1];
NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url1];
// 请求方式 如果不写 默认是 GET
request. HTTPMethod = @"GET" ;
// 当需要请求头时 需要使用可变的 request
[request addValue : @"c2e76db5c772e8fb78b6f4731de39cd1" forHTTPHeaderField : @"apikey" ];
#pragma mark---request 示例 2 、考虑缓存策略 超时限制
/*
cachePolicy:
NSURLRequestUseProtocolCachePolicy , 默认的缓存策略 如果缓存不存在 直接从服务端获取 如果缓存存在 会根据 response 中的 Cache-Control 字段判断下一步操作
NSURLRequestReloadIgnoringCacheData 忽略缓存 重新请求
NSURLRequestReturnCacheDataElseLoad 有缓存就使用 不管其有效性 没有缓存就重新请求
NSURLRequestReturnCacheDataDontLoad 有缓存就用缓存 没有缓存也不发请求 当做请求出错处理(用于离线模式)
timeoutInterval :超时的时间间隔 单位:秒
*/
NSString *urlString = @"http://apis.baidu.com/apistore/weatherservice/citylist?cityname=zhengzhou" ;
NSMutableURLRequest *request1 = [ NSMutableURLRequest requestWithURL :[ NSURL URLWithString :urlString] cachePolicy : NSURLRequestReloadIgnoringCacheData timeoutInterval : 15 ];
request1. HTTPMethod = @"GET" ;
[request1 addValue : @"c2e76db5c772e8fb78b6f4731de39cd1" forHTTPHeaderField : @"apikey" ];