IOS网络协议HTTP
1、网络层基础知识
1.1、HTTP
协议 | 层级 | 连接性 | 可靠性 | 应用场景 |
TCP | 传输层 | 面向连接 | 高 | 文件传输、网页浏览 |
UDP | 传输层 | 无连接 | 低 | 实时通信、流媒体 |
HTTP | 应用层 | 基于TCP | 由TCP保证 | 网页浏览、API通信 |
HTTP通过过程
- ④⑤ 是应用层通信,①②③⑥⑦⑧⑨是运输层通信
- ①②③是三次握手建立通信,⑥⑦是断开Client->Server的通信,⑧⑨是断开Server->Client的通信
HTTP报文格式
1.2、HTTPS
属性 | HTTP | HTTPS |
安全性 | 明文传输,容易被窃听和篡改 | 数据加密,提供机密性和完整性 |
端口 | 80 | 443 |
加密协议 | 无 | 使用SSL/TLS加密 |
身份验证 | 无 | 提供数字证书,验证服务器身份 |
性能 | 快 | 慢 |
使用场景 | 低敏数据传输,普通网页流岚 | 高敏数据传输,登录、支付页面 |
- SSL 是早期的安全通信协议,已被逐步淘汰
- TLS 是 SSL 的改进版,提供更高的安全性和性能
HTTP与HTTPS的区别
SSL通信建立过程
客户端验证服务端证书有两点内容:
- 验证服务端证书的数字摘要 和 服务端证书解密之后的内容 是否一致
- 验证证书链的根证书(一般保存在浏览器或操作系统中)是否在可信任证书列表中
2、代码示例
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
//手机号输入框
@property(nonatomic, strong) UITextField *etPhone;
//搜索按钮
@property(nonatomic, strong) UIButton *btnSearch;
//结果展示
@property(nonatomic, strong) UITextView *tvResult;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initUI];
}
-(void)initUI{
//手机号输入框
self.etPhone = [[UITextField alloc]init];
self.etPhone.borderStyle = UITextBorderStyleRoundedRect;
self.etPhone.placeholder = @"请输入手机号";
self.etPhone.keyboardType = UIKeyboardTypeNumberPad;
self.etPhone.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.etPhone];
//搜索按钮
self.btnSearch = [UIButton buttonWithType:UIButtonTypeSystem];
self.btnSearch.backgroundColor = [UIColor redColor];
[self.btnSearch setTitle:@"查询归属地" forState:UIControlStateNormal];
self.btnSearch.titleLabel.font = [UIFont boldSystemFontOfSize:24];
[self.btnSearch setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.btnSearch.translatesAutoresizingMaskIntoConstraints = NO;
[self.btnSearch addTarget:self action:@selector(searchPhoneNumber) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnSearch];
//结果展示
self.tvResult = [[UITextView alloc]init];
self.tvResult.backgroundColor = [UIColor whiteColor];
self.tvResult.layer.borderColor = [UIColor grayColor].CGColor;
self.tvResult.layer.borderWidth = 1.0;
self.tvResult.layer.cornerRadius = 6.0;
[self.tvResult setTextColor:[UIColor blackColor]];
self.tvResult.scrollEnabled = YES;
self.tvResult.font = [UIFont systemFontOfSize:24];
self.tvResult.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.tvResult];
//设置布局-约束布局
[NSLayoutConstraint activateConstraints:@[
//手机号输入框
[self.etPhone.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20],
[self.etPhone.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20],
[self.etPhone.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-20],
[self.etPhone.heightAnchor constraintEqualToConstant:40],
//搜索按钮
[self.btnSearch.topAnchor constraintEqualToAnchor:self.etPhone.bottomAnchor constant:30],
[self.btnSearch.leadingAnchor constraintEqualToAnchor:self.etPhone.leadingAnchor constant:20],
[self.btnSearch.trailingAnchor constraintEqualToAnchor:self.etPhone.trailingAnchor constant:-20],
[self.btnSearch.heightAnchor constraintEqualToConstant:40],
//结果展示文本
[self.tvResult.topAnchor constraintEqualToAnchor:self.btnSearch.bottomAnchor constant:30],
[self.tvResult.leadingAnchor constraintEqualToAnchor:self.etPhone.leadingAnchor],
[self.tvResult.trailingAnchor constraintEqualToAnchor:self.etPhone.trailingAnchor],
[self.tvResult.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-20]
]];
}
//搜索手机号
-(void)searchPhoneNumber{
NSString *phoneNumber = self.etPhone.text;
if (phoneNumber == nil || phoneNumber.length < 1) {
self.tvResult.text = @"手机号不可为空";
return;
}
NSString *urlStr = [NSString stringWithFormat:@"https://cx.shouji.360.cn/phonearea.php?number=%@", phoneNumber];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error){
//请求失败
dispatch_async(dispatch_get_main_queue(), ^{
self.tvResult.text = [NSString stringWithFormat:@"请求失败: %@", error.localizedDescription];
});
return;
}
NSError *jsonError;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError) {
//json解析异常
dispatch_async(dispatch_get_main_queue(), ^{
self.tvResult.text = [NSString stringWithFormat:@"参数解析失败:%@", jsonError.localizedDescription];
});
return;
}
NSDictionary *dataDict = responseDict[@"data"];
if (dataDict) {
NSString *provice = dataDict[@"province"]?:@"未知";
NSString *city = dataDict[@"city"]?:@"未知";
NSString *sp = dataDict[@"sp"]?:@"未知";
NSString *result = [NSString stringWithFormat:@"归属地:%@ %@\n运营商:%@", provice, city, sp];
dispatch_sync(dispatch_get_main_queue(), ^{
self.tvResult.text = result;
});
}else{
dispatch_sync(dispatch_get_main_queue(), ^{
self.tvResult.text = @"未查询到结果";
});
}
}];
[dataTask resume];
}
@end