NSURLConnection在HTTP 401之后不调用NSURLConnectionDelegate身份验证方法

时间:2013-06-19 15:35:55

标签: ios nsurlconnection http-authentication nsurlconnectiondelegate nsurlcredential

我正在使用一个使用带有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的身份验证回调?

1 个答案:

答案 0 :(得分:1)

我的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对此有所了解。请复制它。

显然,我必须自NSURLConnection will not allow you to proactively authenticate with NSURLCredential以来手动设置Authorization标题。