2

我正在尝试在 UIWebView 中加载安全网站,我的基本方法是创建一个 NSURL、na 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 不会完成然后重新加载视图,则不会发送身份验证质询,但会加载视图。我对服务器没有任何控制权。我愿意使用 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];
}

我哪里做错了?

4

4 回答 4

4

您需要首先检索服务器请求的“身份验证方法”:

[[challenge protectionSpace] authenticationMethod]

这些是上面的表达式返回的身份验证方法(它们是字符串常量):

NSURLAuthenticationMethodDefault
NSURLAuthenticationMethodHTTPBasic
NSURLAuthenticationMethodHTTPDigest
NSURLAuthenticationMethodHTMLForm
NSURLAuthenticationMethodNegotiate
NSURLAuthenticationMethodNTLM
NSURLAuthenticationMethodClientCertificate
NSURLAuthenticationMethodServerTrust

然后,您有以下选择:

  1. 如果要为给定的身份验证方法提供凭据,请调用 useCredential:forAuthenticationChallenge:

  2. 如果您不想自己处理该身份验证方法并希望系统尝试进行身份验证,您可以调用 performDefaultHandlingForAuthenticationChallenge: 可能会失败与否,具体取决于系统是否能够处理该类型的身份验证以及它是否可以很好地找到凭据已知的存储。

  3. 如果您无法处理该身份验证方法(例如身份验证方法 NSURLAuthenticationMethodNTLM),您可以跳过此保护空间并尝试另一个保护空间(如果此身份验证质询中存在另一个保护空间)。然后您可能会获得NSURLAuthenticationMethodHTTPBasic您能够处理的身份验证方法。为了拒绝当前的保护空间,您将方法发送 rejectProtectionSpaceAndContinueWithChallenge:给身份验证质询发送者。然后,如果还有其他保护空间,NSURLConnection将再次发送给您的代表。willSendRequestForAuthenticationChallenge:

  4. 您可以尝试在不提供凭据的情况下继续。很可能,身份验证将失败。您可以通过向continueWithoutCredentialForAuthenticationChallenge: 身份验证挑战发送者发送消息来尝试。

  5. 最后,您可以通过取消身份验证质询来取消请求:发送cancelAuthenticationChallenge:给身份验证质询发送者。

注意:NSURLAuthenticationMethodHTTPBasicNSURLAuthenticationMethodHTTPDigest身份验证方法可以使用NSURLCredential创建的相同对象来处理+credentialWithUser:password:persistence:

于 2013-06-15T12:00:06.370 回答
0

如果有人出现并遇到同样的问题,请确保我想分享我找到的解决方案。使用 AFNetworking。

这是修改后的代码:

-(void)updateCard
{
  if(!self.webView)
  {
    self.webView =[[UIWebView alloc] initWithFrame:self.bounds];
    self.webView.delegate = self;
  }
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  NSString *username = @"username";
  NSString *password = @"password";
  NSURL *url = [NSURL URLWithString:@"https://ssl.letu.edu/"];
  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];

  [client setAuthorizationHeaderWithUsername:username password:password];

  NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"applications/chapelattendance/attendance.html"
                                                parameters:nil];

  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
  {
    [self.webView loadRequest:request];
  }
                                   failure: ^(AFHTTPRequestOperation *operation, NSError *error)
  {
    NSLog(@"Could not load chapel attendance");
  }];

  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  [queue addOperation:operation];
}
于 2013-06-15T06:04:02.547 回答
0

AFHTTPRequestOperation *操作 = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

手术。securityPolicy.allowInvalidCertificates = YES

于 2014-08-26T07:25:00.937 回答
0

您需要发送带有 http 标头的用户名和密码组合以在发送请求时对请求进行身份验证。

NSData *authData = [@"username:password" dataUsingEncoding:NSASCIIStringEncoding];

NSString *authorization = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];

[mutableRequest addValue:authorization forHTTPHeaderField:@"Authorization"];
于 2014-09-19T14:58:02.480 回答