ios - 如何使用 Alamofire 发送带有 gzip 内容的 HTTP POST 请求?

标签 ios http post gzip alamofire

我正在开发一个 iOS 应用程序,用于将大约 2000 个对象数组发布到服务器。我在做一个实验。我用过

let param: [String: AnyObject] = [
    "OwnerEmail": document.fromOwner!.email as AnyObject,
    "UTCTimeStamp": document.utcTimeStamp as AnyObject,
    "Latitude": document.latitude as AnyObject,
    "Longitude": document.longitude as AnyObject,
    ...
]

customAlamofireManager.request(<ServerURL>, 
    method: .post, 
    parameters: param, 
    encoding: JSONEncoding.default, 
    headers: headers)

发布 1 个对象。但是现在我发布了 2000 个或更多,我正在考虑使用 gzip压缩对象数组,然后上传以检查网络效率。但我不知道如何使用 Alamofire 做到这一点。 我找到了这个 link .

但是,Alamofire 现在最新版本中没有枚举 ParameterEncoding。其他一些链接建议使用 Alamofire.upload。 感谢您的帮助。

最佳答案

您可以使用以下代码通过 Alamofire SDK 传递 POST 请求的参数:

 let param: [String: AnyObject] = [
        "OwnerEmail": document.fromOwner!.email as AnyObject,
        "UTCTimeStamp": document.utcTimeStamp as AnyObject,
        "Latitude": document.latitude as AnyObject,
        "Longitude": document.longitude as AnyObject,
        ...
    ]
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    do{
        let data = try JSONSerialization.data(withJSONObject: param, options: .prettyPrinted)
        request.httpBody = data

        Alamofire.request(request).responseJSON { (response) in


            print(response)

        }

    } catch
        {
       }

这将有助于您解决上述问题。

关于ios - 如何使用 Alamofire 发送带有 gzip 内容的 HTTP POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43062346/

相关文章:

iOS 检测 UIWebView 中显示的 PDF 中的 URL

ios - 如何让我的 Swift 类实时更新 View Controller 中的标签?

ios - Xcode 9 的多发布环境 - Swift

http - “刷新”HTTP header

angular - 将 cordova-http 插件 Promise 包装为可观察的

http - Golang路由前修改URL路径等HTTP请求参数

PHP上传4张图片到文件和数据库MYSQL的路径

ios - 以编程方式创建不同屏幕尺寸下的对象

jquery - asp .net mvc3 + 使用 $.ajax post 时检查用户是否登录

swift - 带有参数和 header 的 Alamofire 多部分形式图像上传 - Swift