我试图在 UIWebView 中加载一个安全网站,我的基本方法是创建一个 NSURL,然后创建一个 NSURLRequest,然后创建一个 NSURLConnection,然后在 UIWebView 中加载 NSURLRequest。当网站加载时,我收到
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
我回复挑战发送者
- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
但之后我什么也没得到......它只是挂起。我设置了断点,所以我知道
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
正在被调用。如果我等到确定 NSURLConnection 不会完成然后重新加载 View ,则不会发送身份验证质询,但 View 将加载。我对服务器没有任何控制权。我愿意使用 AFNetworking,但仅限于必要时。
下面提供了完整的源代码列表:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:
(NSURLAuthenticationChallenge *)challenge
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([challenge previousFailureCount] == 0)
{
NSString *username = @"username";
NSString *password = @"passsword";
NSURLCredential * cred = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
}
else
{
}
}
-(void)updateCard
{
NSURL * url = [NSURL URLWithString:@"https://ssl.letu.edu/applications/chapelattendance/attendance.html"];
NSURLRequest * request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:50.0];
self.webView =[[UIWebView alloc] initWithFrame:self.bounds];
self.webView.delegate = self;
[self.webView loadRequest:request];
self.connection = [[ NSURLConnection alloc]initWithRequest:request delegate:self];
[self.connection start];
}
我哪里出错了?
最佳答案
您需要首先检索服务器请求的“身份验证方法”:
[[challenge protectionSpace] authenticationMethod]
这些是上面的表达式返回的身份验证方法(字符串常量):
NSURLAuthenticationMethodDefault
NSURLAuthenticationMethodHTTPBasic
NSURLAuthenticationMethodHTTPDigest
NSURLAuthenticationMethodHTMLForm
NSURLAuthenticationMethodNegotiate
NSURLAuthenticationMethodNTLM
NSURLAuthenticationMethodClientCertificate
NSURLAuthenticationMethodServerTrust
然后,您有以下选择:
如果您想为给定的身份验证方法提供凭据,请调用
useCredential:forAuthenticationChallenge:
如果您不想自己处理该身份验证方法并希望系统尝试 要进行身份验证,您可以调用
performDefaultHandlingForAuthenticationChallenge:
然后可能会失败,具体取决于系统是否能够处理该类型 身份验证以及是否可以在众所周知的存储中找到凭据。如果您无法处理该身份验证方法——请说身份验证方法 例如
NSURLAuthenticationMethodNTLM
- 您可以跳过此保护 空间并尝试另一个保护空间(如果有) 存在于此身份验证质询中。那么你可能会得到一个 您使用的身份验证方法NSURLAuthenticationMethodHTTPBasic
有能力处理。 为了拒绝当前保护空间你发送的方法rejectProtectionSpaceAndContinueWithChallenge:
到 身份验证质询发送者。然后,NSURLConnection
将发送 再次willSendRequestForAuthenticationChallenge:
发送至您的 如果存在进一步的保护空间,则委托(delegate)给另一个保护空间。您可以尝试在不提供凭据的情况下继续。 身份验证很可能会失败。你可以尝试通过 发送消息
continueWithoutCredentialForAuthenticationChallenge:
发送给身份验证质询发送者。最后,您可以通过取消 身份验证质询:发送
cancelAuthenticationChallenge:
至 身份验证质询发送者。
注意:NSURLAuthenticationMethodHTTPBasic
和 NSURLAuthenticationMethodHTTPDigest
身份验证方法可以使用使用 +credentialWithUser:password:persistence 创建的同一
NSURLCredential
对象来处理:
关于ios - NSURLConnection 仅在第二次尝试时进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17120392/