zoukankan      html  css  js  c++  java
  • Alamofire请求网络

    HTTP - GET和POST请求
    - 如果要传递大量数据,比如文件上传,只能用POST请求
    - GET的安全性比POST要差些,如果包含机密/敏感信息,建议用POST
    - 如果仅仅是索取数据(数据查询),建议用GET
    - 如果是增加、修改、删除数据,建议使用POST

    iOS中的HTTP
    - NSURLConnection:用法简单,最古老最经典最直接的一种方案
    - NSURLSession:iOS7新出的技术,功能比NSURLConnection更加强大
    - AFNetworking:简单易用,提供了基本够用的常用功能
    - Alamofire:纯Swift网络请求库

    Alamofire特点:
    1.纯Swift编写的HTTP网络库
    2.链式请求、响应
    3.URL/JSON/plist格式参数
    4.上传文件/数据/流/多格式数据
    5.断点续传
    6.NSURLCredential授权
    7.HTTP响应验证
    8.NSProgress&进度闭包

    使用CocoaPods安装:

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '9.0'
    use_frameworks!
    target 'AlamofireDemo' do
    pod 'Alamofire'
    pod 'SwiftyJSON'
    end
    • 请求:NSURLRequest
    • 回复:NSHTTPURLResponse
    • 数据:NSData
    • 错误:NSError

    Alamofire基本用法:

    import Alamofire
    Alamofire.request(.GET,"https://httpbin.org/get")

    响应处理:

    响应序列化:

    response()
    responseData()
    responseString(encoding:NSStringEncoding)
    responseJSON(options:NSJSONReadingOptions)
    responsePropertyList(options:NSPropertyListReadOptions)

    HTTP方法:

    public enum Method:String{
    case OPTIONS,GET,HEAD,POST,PUT,PATCH,DELETE,TRACE,CONNECT
    }

    参数:

    let parameters=[
        "foo":"bar",
        "baz":["a",1],
        "qux":[
            "x":1,
            "y":2,
            "z":3
        ]
    ]

    参数Encoding和Content-Type

    enum ParameterEncoding{
    case URL    //application/x-www-form-urlencoded
    case JSON //application/json
    case PropertyList //application/x-plist
    }

    HHTP Headers:

    支持的上传类型:

    1.File

    2.Data

    3.Stream

    4.MultipartFormData

    下载代码演示:

    let destination = 
     Alamofire.Request.suggestedDownloadDestination(directory:.DocumentDirectory,domain:.UserDomainMask)
     Alamofire.download(.GET,"https://httpbin.org/stream/100",destinaion:destination)

    认证:

    - Authentication(认证)和Authorization(授权)

    - NSURLCredential and NSURLAuthenticationChallenge

    - 认证协议

     1.HTTP Basic(每次发送相同认证信息)

     2.HTTP Digest(每次发送的认证信息不一样) 

    验证(validation):

    - Alamofire默认不验证响应的内容一律认为请求成功,需要验证

    - 手动验证

    - 自动验证

    - status code:200...299

    - 响应的Content-Type必须匹配请求头

    Request对象:

    - request,upload或者download

    - authenticate,validate和responseData

    - suspend()

    - resume()

    - cancel()会产生错误传递给已经注册的所有响应处理者

    Response解析:

    - strings,JSON,and property lists

    - 自定义对象解析

    - 自定义对象集合解析

    URLStringConvertible:

     具体代码实现传送门:AlamofireDemo

  • 相关阅读:
    【leetcode_easy】530. Minimum Absolute Difference in BST
    【leetcode】521. Longest Uncommon Subsequence I
    【leetcode】520. Detect Capital
    【leetcode】509. Fibonacci Number
    【leetcode】507. Perfect Number
    第2章 GNS3和PacketTracer网络模拟器(1)_GNS3概述
    第1章 计算机网络和协议(4)_网络性能指标
    第1章 计算机网络和协议(3)_TCP/IP协议
    第1章 计算机网络和协议(2)_OSI参考模型
    第1章 计算机网络和协议(1)_网络的概念
  • 原文地址:https://www.cnblogs.com/abelsu/p/5655640.html
Copyright © 2011-2022 走看看