NSURLConnectionDelegate

NSURLConnectionDelegate

我正在使用通过NSURLConnection使用HTTP基本身份验证的Web服务。在我的NSURLConnectionDelegate中,我实现了–[NSURLConnectionDelegate connection:canAuthenticateAgainstProtectionSpace:]
–[NSURLConnectionDelegate connection:didCancelAuthenticationChallenge:],以及
–[NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge:],但没有一个被调用。

我尝试实现–[NSURLConnectionDelegate connectionShouldUseCredentialStorage:]来返回YESNO,但这没有效果。

I also ensured that my NSURLCredentialStorage was empty

最后,代替实现–[NSURLConnectionDelegate connection:canAuthenticateAgainstProtectionSpace:]
–[NSURLConnectionDelegate connection:didCancelAuthenticationChallenge:],以及
–[NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge:],我实现了–[NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:],但是没有被调用。

如何使NSURLConnection调用NSURLConnectionDelegate的身份验证回调?

最佳答案

我的HTTP服务器未返回WWW-Authenticate标头。

我的服务器的响应:

HTTP/1.1 401 Unauthorized
Date: Wed, 19 Jun 2013 14:43:30 GMT
Server: Apache-Coyote/1.1
Expires: Wed, 19 Jun 2013 14:43:30 GMT
Vary: Accept-Encoding,User-Agent
Cache-Control: no-store, no-cache, must-revalidate, private, max-age=0
Content-Length: 0
Content-Type: text/plain


另一个带有WWW-Authenticate标头的服务器响应:

HTTP/1.1 401 Authorization Required
Date: Wed, 19 Jun 2013 14:44:47 GMT
Server: Apache/2.2.17 (CentOS)
WWW-Authenticate: Basic realm="[LDAP/PROD] Active Directory"
Content-Length: 0
Connection: close
Content-Type: text/plain


看起来NSURLConnection在响应中需要WWW-Authenticate标头才能调用NSURLConnectionDelegate的身份验证回调。

我对此filed a radar。请复制它。

显然,从Authorization will not allow you to proactively authenticate with NSURLConnection开始,我将必须手动设置NSURLCredential标头。

09-25 18:45