一、一个HTTP请求的基本要素
1.请求URL:客户端通过哪个路径找到服务器
2. 请求参数:客户端发送给服务器的数据
* 比如登录时需要发送的用户和密码
3.返回结果:服务器返回给客户端的数据
* 一般是JSON数据或者XML
二、基本的HTTP请求的步骤(移动客户端)
1.拼接"请求URL" + "?" + "请求参数"
* 请求参数的格式:参数名=参数值
* 多个请求参数之间用&隔开:参数名1=参数名1&参数名2=参数值2
* 比如:http://localhost:8080/MJServer/login?username=123?pwd=456
2. 发送请求
3. 解析服务器返回的数据
三、HTTP的请求和响应
1. 请求
1> 请求行
* 请求方法、请求路径、HTTP协议的版本
GET /MJServer/login?username=123&pwd=123&method=get&type=JSON HTTP/1.1
2> 请求头(Request Header)
* 客户端的一些信息
Host: localhost:8080 // 主机名
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0 // 客户端的运行环境(软件环境)
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 // 支持的接收格式
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 // 浏览器所用的语言
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/MJServer/
Connection: keep-alive // TCP连接在发送后将仍然保持打开状态
3> 请求体(请求正文):只有POST才有
* 请求参数,发给服务器的数据
username=123&pwd=123&method=post&type=JSON
2. 响应
1> 状态行(响应行):HTTP协议的版本、响应状态码、状态码的描述
HTTP/1.1 200 OK
2> 响应头:服务器的一些信息
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8 // 服务器返回给客户端的内容类型
Transfer-Encoding: chunked
Date: Tue, 20 Oct 2015 04:46:36 GMT
Content-Length : 服务器返回给客户端内容的长度(比如文件的大小)
3> 实体内容(响应体)
* 服务器返回给客户端具体的数据,比如文件数据
{"success":"登录成功"}
四、HTTP请求的方式:
1.GET
* 所有的参数都拼接在请求头上,即拼接在url上
* 请求头的长度有限,一般为1kB
* 容易暴露用户的个人信息
* 一般读取服务器信息, 使用GET
2. POST
* 所有参数放在请求体中,不会暴露在url
* 请求体理论上讲长度没有限制,具体得看服务器
* 相对来说,安全性更高
* 如果用户用到隐私参数或者敏感参数一般使用POST,保证安全
* 一般除了读取服务器信息外,都使用POST
五、如何发送一个HTTP请求
1. 发送GET请求(默认就是GET请求)
// 1.获取url
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video?type=XML"];
// 2.创建请求
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
// 3.创建session
NSURLSession *session = [NSURLSession sharedSession];
// 4.创建task
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}
// 5.发送请求
[task resume];
2. 发送POST请求
// 1.获取url
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// 2.创建请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
// 3.设置请求方式
request.HTTPMethod = @"POST";
// 4.设置请求题
NSString *prame = [NSString stringWithFormat:@"username=%@&pwd=%@", @"123", @"123"];
// NSString --> NSData
request.HTTPBody = [prame dataUsingEncoding:NSUTF8StringEncoding];
// 5.设置请求头信息
[request setValue:@"iPhone 7s" forHTTPHeaderField:@"User-Agent"];
// 6.创建session 和 task
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}
// 7.发送请求
[task resume];
六、NSMutableURLReqst的常用方法
1> 设置超时(默认超时的时间的60s)
requst.timeoutInterval = 5;
七、URL转码(URL不能包含中文,得对中文转码)
NSString *urlStr = @"http://localhost:8080/MJServer/video";
// 转码
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
八、密码加密
1.网络数据加密
1> 加密对象:隐私数据,比如密码、银行信息
2> 加密方案
* 提交数据, 必须使用POST请求
* 使用加密算法对隐私数据进行加密,比如MD5
3> 加密增强:为了加大破解的难度
* 对铭文进行2次MD5 : MD5(MD5($pass))
* 先对明文撒盐,再进行MD5: MD5($pass.$salt)
2. 本地存储加密
1> 加密对象 :重要的数据,比如游戏数据
3. 代码安全问题
1> 现在已经有工具盒技术能反编译出源代码: 逆向工程
* 反编译出来的都是纯C语言的,可读性不高
* 做起码能知道源代码里面用的是哪些框架
2> 参考书籍:《iOS逆向工程》
3> 解决方案: 发布之前对代码进行混淆
* 混淆前
@interface PYPerson :NSObject
- (void)run;
- (void)eat;
@end
* 混淆后
@interface A :NSObject
- (void)a;
- (void)b;
@end
九、网络状态的监听
1. 主动监听网络状态
// 是否是WIFI
+ (BOOL)isEnableWIFI{
return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable;
}
// 是否是手机移动网络
+ (BOOL)isEnable4G{
return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable
}
2. 监听网络状态
1> 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChange) name:kReachabilityChangedNotification object:nil];
2> 开始监听网络状态
// 创建一个reachability
self.reachility = [Reachability reachabilityForInternetConnection];
// 开始监听
[self.reachility startNotifier];
3> 移除监听
// 停止监听
[self.reachility stopNotifier];
// 移除监听者
1.请求URL:客户端通过哪个路径找到服务器
2. 请求参数:客户端发送给服务器的数据
* 比如登录时需要发送的用户和密码
3.返回结果:服务器返回给客户端的数据
* 一般是JSON数据或者XML
二、基本的HTTP请求的步骤(移动客户端)
1.拼接"请求URL" + "?" + "请求参数"
* 请求参数的格式:参数名=参数值
* 多个请求参数之间用&隔开:参数名1=参数名1&参数名2=参数值2
* 比如:http://localhost:8080/MJServer/login?username=123?pwd=456
2. 发送请求
3. 解析服务器返回的数据
三、HTTP的请求和响应
1. 请求
1> 请求行
* 请求方法、请求路径、HTTP协议的版本
GET /MJServer/login?username=123&pwd=123&method=get&type=JSON HTTP/1.1
2> 请求头(Request Header)
* 客户端的一些信息
Host: localhost:8080 // 主机名
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0 // 客户端的运行环境(软件环境)
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 // 支持的接收格式
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 // 浏览器所用的语言
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/MJServer/
Connection: keep-alive // TCP连接在发送后将仍然保持打开状态
3> 请求体(请求正文):只有POST才有
* 请求参数,发给服务器的数据
username=123&pwd=123&method=post&type=JSON
2. 响应
1> 状态行(响应行):HTTP协议的版本、响应状态码、状态码的描述
HTTP/1.1 200 OK
2> 响应头:服务器的一些信息
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8 // 服务器返回给客户端的内容类型
Transfer-Encoding: chunked
Date: Tue, 20 Oct 2015 04:46:36 GMT
Content-Length : 服务器返回给客户端内容的长度(比如文件的大小)
3> 实体内容(响应体)
* 服务器返回给客户端具体的数据,比如文件数据
{"success":"登录成功"}
四、HTTP请求的方式:
1.GET
* 所有的参数都拼接在请求头上,即拼接在url上
* 请求头的长度有限,一般为1kB
* 容易暴露用户的个人信息
* 一般读取服务器信息, 使用GET
2. POST
* 所有参数放在请求体中,不会暴露在url
* 请求体理论上讲长度没有限制,具体得看服务器
* 相对来说,安全性更高
* 如果用户用到隐私参数或者敏感参数一般使用POST,保证安全
* 一般除了读取服务器信息外,都使用POST
五、如何发送一个HTTP请求
1. 发送GET请求(默认就是GET请求)
// 1.获取url
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video?type=XML"];
// 2.创建请求
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
// 3.创建session
NSURLSession *session = [NSURLSession sharedSession];
// 4.创建task
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}
// 5.发送请求
[task resume];
2. 发送POST请求
// 1.获取url
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
// 2.创建请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
// 3.设置请求方式
request.HTTPMethod = @"POST";
// 4.设置请求题
NSString *prame = [NSString stringWithFormat:@"username=%@&pwd=%@", @"123", @"123"];
// NSString --> NSData
request.HTTPBody = [prame dataUsingEncoding:NSUTF8StringEncoding];
// 5.设置请求头信息
[request setValue:@"iPhone 7s" forHTTPHeaderField:@"User-Agent"];
// 6.创建session 和 task
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
}
// 7.发送请求
[task resume];
六、NSMutableURLReqst的常用方法
1> 设置超时(默认超时的时间的60s)
requst.timeoutInterval = 5;
七、URL转码(URL不能包含中文,得对中文转码)
NSString *urlStr = @"http://localhost:8080/MJServer/video";
// 转码
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
八、密码加密
1.网络数据加密
1> 加密对象:隐私数据,比如密码、银行信息
2> 加密方案
* 提交数据, 必须使用POST请求
* 使用加密算法对隐私数据进行加密,比如MD5
3> 加密增强:为了加大破解的难度
* 对铭文进行2次MD5 : MD5(MD5($pass))
* 先对明文撒盐,再进行MD5: MD5($pass.$salt)
2. 本地存储加密
1> 加密对象 :重要的数据,比如游戏数据
3. 代码安全问题
1> 现在已经有工具盒技术能反编译出源代码: 逆向工程
* 反编译出来的都是纯C语言的,可读性不高
* 做起码能知道源代码里面用的是哪些框架
2> 参考书籍:《iOS逆向工程》
3> 解决方案: 发布之前对代码进行混淆
* 混淆前
@interface PYPerson :NSObject
- (void)run;
- (void)eat;
@end
* 混淆后
@interface A :NSObject
- (void)a;
- (void)b;
@end
九、网络状态的监听
1. 主动监听网络状态
// 是否是WIFI
+ (BOOL)isEnableWIFI{
return [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable;
}
// 是否是手机移动网络
+ (BOOL)isEnable4G{
return [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable
}
2. 监听网络状态
1> 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChange) name:kReachabilityChangedNotification object:nil];
2> 开始监听网络状态
// 创建一个reachability
self.reachility = [Reachability reachabilityForInternetConnection];
// 开始监听
[self.reachility startNotifier];
3> 移除监听
// 停止监听
[self.reachility stopNotifier];
// 移除监听者
[[NSNotificationCenter defaultCenter] removeObserver:self];
iOS9常见的错误描述:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app‘s Info.plist file.
原因:在iOS9 beta中,苹果将原http协议改成了https协议,使用 TLS1.2 SSL加密请求数据。
解决方法:
在info.plist 加入key
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>