我在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结果。
为什么会发生这种情况?
答案 0 :(得分:2)
看起来像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)
}
}
答案 1 :(得分:0)
当图书馆不能以奇异,意外或荒谬的方式工作时,库中可能存在错误。如果您使用的是不稳定版本或git存储库快照,请务必检查上游树中的修复,即使您认为库工作正常。
所以这是通过更新库来解决的。当前(在撰写本文时)库中不再存在HTTPAuth
。它由用户提供的闭包替换,如library documentation:
var request = HTTPTask()
//the auth closures will continually be called until a successful auth or rejection
var attempted = false
request.auth = {(challenge: NSURLAuthenticationChallenge) in
if !attempted {
attempted = true
return NSURLCredential(user: "user", password: "passwd", persistence: .ForSession)
}
return nil //auth failed, nil causes the request to be properly cancelled.
}