我在iOS应用程序中使用SwiftHTTP在web服务器中执行基本身份验证。如果身份验证成功,则一切正常工作。但是,如果身份验证失败(代码401),则调用失败处理程序大约需要35到45秒。代码如下:
var request = HTTPTask()
auth = HTTPAuth(username: user, password: password)
auth!.persistence = .ForSession
request.auth = auth
request.requestSerializer.timeoutInterval = 5
request.GET("http://\(ipaddr)", parameters: nil,
success: {(response : HTTPResponse) in
NSLog("login success")
},
failure: {(error: NSError, response: HTTPResponse?) in
NSLog("login failure")
}
)
请注意,超时值设置为5秒。检查SwiftHTTP代码,实际的请求实现可以归结为:
var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
我在SwiftHTTP中添加了一些调试行,以检查是否正确设置了
timeoutInterval
。我尝试了缓存策略和其他可配置参数的变化,结果没有变化。我还打开了到HTTP服务器的telnet连接,并手动发送了相同的请求,401结果如预期的那样立即返回。你知道为什么会这样吗?
最佳答案
似乎HTTPTask
可以处理这样的挑战:
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
if let a = auth {
let cred = a(challenge)
if let c = cred {
completionHandler(.UseCredential, c)
return
}
completionHandler(.RejectProtectionSpace, nil)
return
}
completionHandler(.PerformDefaultHandling, nil)
}
我建议您仅在
previousFailureCount
为零时使用凭证,例如。public func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
if let credential = auth?(challenge) {
if challenge.previousFailureCount == 0 {
completionHandler(.UseCredential, credential)
} else {
completionHandler(.RejectProtectionSpace, nil)
}
} else {
completionHandler(.PerformDefaultHandling, nil)
}
}