当前位置: 首页 > article >正文

IOS网络协议HTTP

1、网络层基础知识

1.1、HTTP

协议层级连接性可靠性应用场景
TCP传输层面向连接文件传输、网页浏览
UDP传输层无连接实时通信、流媒体
HTTP应用层基于TCP由TCP保证网页浏览、API通信

HTTP通过过程

  • ④⑤ 是应用层通信,①②③⑥⑦⑧⑨是运输层通信
  • ①②③是三次握手建立通信,⑥⑦是断开Client->Server的通信,⑧⑨是断开Server->Client的通信

HTTP报文格式

1.2、HTTPS

属性HTTPHTTPS
安全性明文传输,容易被窃听和篡改数据加密,提供机密性和完整性
端口80443
加密协议使用SSL/TLS加密
身份验证提供数字证书,验证服务器身份
性能
使用场景低敏数据传输,普通网页流岚高敏数据传输,登录、支付页面
  • SSL 是早期的安全通信协议,已被逐步淘汰
  • TLS 是 SSL 的改进版,提供更高的安全性和性能

HTTP与HTTPS的区别

SSL通信建立过程

客户端验证服务端证书有两点内容:

  1. 验证服务端证书的数字摘要 和 服务端证书解密之后的内容 是否一致
  2. 验证证书链的根证书(一般保存在浏览器或操作系统中)是否在可信任证书列表中

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


http://www.kler.cn/a/502217.html

相关文章:

  • Pandas常用数据类型
  • 每天五分钟深度学习框架pytorch:快速搭建VGG网络的基础模块VGG块
  • LeetCode 2270.分割数组的方案数:前缀和
  • 后盾人JS--JS值类型使用(终章)
  • 浅谈计算机网络01 | 计算机网络数据平面
  • mycat介绍与操作步骤
  • 备战蓝桥杯:树的存储与遍历(dfs和bfs)
  • lerna使用指南
  • Android中的Service
  • Docker的CMD指令
  • VMware虚拟机安装Home Assistant智能家居平台并实现远程访问保姆级教程
  • Android切换语言不退出App
  • 一个可以把玩的针对WebSocket分段的处理方案
  • 浅谈云计算07 | 云安全机制
  • 蓝桥杯历届真题 # 数字诗意(C++,Java)
  • React面试常见题目
  • C++中 为什么要把基类指针指向子类对象?
  • STM32 FreeRTOS的任务创建和删除
  • 2_CSS3 背景 --[CSS3 进阶之路]
  • vue集成导出 txt文本文档 和 excel文档 的方法
  • jsdom爬虫程序中eBay主页内容爬取的异步处理
  • 从epoll事件的视角探讨TCP:三次握手、四次挥手、应用层与传输层之间的联系
  • 【论文笔记】Sign Language Video Retrieval with Free-Form Textual Queries
  • IDEA的Git界面(ALT+9)log选项不显示问题小记
  • 基于Java的推箱子游戏设计与实现
  • 31_搭建Redis分片集群