iOS - 带摘要的网络调用

标签 ios swift alamofire sdwebimage digest-authentication

因此我们开始在项目中使用摘要机制。 在 vc 中我有以下方法:

  func imageThumbnailcall() {

    self.indicaotrTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.HandleOverTime), userInfo: nil, repeats: false)
    self.backgroundImageTimer = Timer.scheduledTimer(withTimeInterval: 0.09, repeats: true, block: { (timer) in

        SDWebImageDownloader.shared().setValue("\(UserDefaults.standard.string(forKey: "imageHeader") ?? "YWRtaW46YWRtaW4=")", forHTTPHeaderField: "Authorization")

        SDWebImageDownloader.shared().downloadImage(with:(self.isShowingThermal ? self.thermalUrl : self.visualUrl) as URL, options: [], progress: nil, completed: { (image, data, error, status) in
            self.imageLoaderIndicator.stopAnimating()
            self.backGroundImageView.image = image
        })
    })

}

在上面的调用中,我需要将 header 替换为摘要 header ,因此我进行了 alamofire 调用,收到 401 错误并将其作为 header :

 - some : <NSHTTPURLResponse: 0x1d42265a0> { URL: http://192.168.42.1/images/snapshots/DLTVimage.jpeg } { Status Code: 401, Headers {
Connection =     (
    "keep-alive"
);
"Content-Length" =     (
    195
);
"Content-Type" =     (
    "text/html"
);
Date =     (
    "Tue, 02 Oct 2018 13:22:25 GMT"
);
"Www-Authenticate" =     (
    "Digest algorithm=\"MD5\", qop=\"auth\", realm=\"Restricted access\", nonce=\"18f42caa5bb37111\""
);
 } }

我的理解是我需要创建一个像这样的字符串:

Digest username="admin",realm="Restricted access",nonce="1d382e075bac95ba",uri="/images/snapshots//IRimage.jpeg",response="54c5aad4e9854201c5e66375f41769d2",cnonce="db860bdba0f3f328",algorithm=MD5,qop=auth,nc=00000001"

并将其用作标题。 我可以这样做吗?有一个 pod 可以帮助解决这个问题吗?

最佳答案

所以我做了以下方法:

func imageStreamTest(url: String, success: @escaping (UIImage) -> Void, failure: @escaping () -> Void) {
    Alamofire.request(url).authenticate(user: UserDefaults.standard.string(forKey: "userName") ?? "", password: UserDefaults.standard.string(forKey: "password") ?? "").responseData { (responseObject) -> Void in

        if responseObject.error != nil {
            return
        }

        if let image1 = responseObject.result.value {
            guard let image = UIImage(data: image1) else { return }
            success(image)
        }
    }
}

Alamofire 正在处理摘要身份验证,我收到的数据为 200。 在后台发生 401 故障并由 alamofire 处理

关于iOS - 带摘要的网络调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52609113/

相关文章:

swift - 无法将类型 'String' 的值转换为预期的参数类型 'TimeInterval'(又名 'Double')

ios - cordova::XMLHttpRequest::setRequestHeader 不适用于 JSONP

ios - 当我希望我的应用程序支持 iOS6 时如何设置基础 SDK

swift - 图片不会异步加载以在swift项目上填充UIImageView

swift - 在元组中保存响应时如何忽略 async let 抛出错误?

ios - 使用 Alamofire 用 JSON 数组填充 UIPickerView 时遇到问题 | swift

ios - Alamofire 请求给出内存警告

ios - Apple Watch Complication 数据应该存储在哪里?

ios - phoneGap (Cordova) 如何在内部工作,iOS 特定

html - 如何将 html 文本设置为标签 Swift