我正在使用通过NSURLConnection
使用HTTP基本身份验证的Web服务。在我的NSURLConnectionDelegate
中,我实现了–[NSURLConnectionDelegate connection:canAuthenticateAgainstProtectionSpace:]
,–[NSURLConnectionDelegate connection:didCancelAuthenticationChallenge:]
,以及–[NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge:]
,但没有一个被调用。
我尝试实现–[NSURLConnectionDelegate connectionShouldUseCredentialStorage:]
来返回YES
和NO
,但这没有效果。
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
标头。