multipart / form-data upload任务不会为iOS中的大文件调用Authentication Challenge

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了multipart / form-data upload任务不会为iOS中的大文件调用Authentication Challenge相关的知识,希望对你有一定的参考价值。

我正在尝试使用URLSession.dataTask(with: r as URLRequest)将zip文件从移动设备上传到远程Azure服务器。为此,我使用NSURLAuthenticationMethodServerTrustNSURLAuthenticationMethodClientCertificate身份验证。当zip文件的大小为~15KB时,两者工作正常。

但对于更大的文件,我只看到NSURLAuthenticationMethodServerTrust然后请求超时。我已经花了3天没有任何具体方向。

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate{
        if useFirstCert, let certURL = Bundle.main.url(forResource: "azure-client1-cert", withExtension: "p12"){
            let cred = credential(from: certURL, password: "passcode")
            completionHandler(URLSession.AuthChallengeDisposition.useCredential, cred)
        }else {
            completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
        }
    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
        completionHandler(URLSession.AuthChallengeDisposition.rejectProtectionSpace, nil)
    }else{
        completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, nil);
    }
}
答案

最后,我得到了微软的回复,如以下博客 - https://blogs.msdn.microsoft.com/waws/2017/04/03/posting-a-large-file-can-fail-if-you-enable-client-certificates/

我刚刚在一个带有124K文件的原型应用中上传了一个成功的证书。所有需要做的就是为请求设置Expect:100-continue标头。

let request = NSMutableURLRequest(url)
request.setValue("100-continue", forHTTPHeaderField: "Expect")

它适用于使用IIS的所有协议。

以上是关于multipart / form-data upload任务不会为iOS中的大文件调用Authentication Challenge的主要内容,如果未能解决你的问题,请参考以下文章

jmeter实现multipart/form-data类型请求

jmeter实现multipart/form-data类型请求

[转]如何使用multipart/form-data格式上传文件

使用 Feign 上传文件 - multipart/form-data

multipart/form-data

jQuery AJAX 'multipart/form-data' 不发送数据?