didReceiveAuthenticationChallenge 导致 EXC_BAD_ACCESS

发布于 2024-08-22 13:43:16 字数 1092 浏览 7 评论 0原文

如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“已创建凭据”后会立即出现 EXC_BAD_ACCESS 错误。我不会发布任何内容,并且此代码是直接从文档中复制的 - 知道出了什么问题吗?

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

  [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}

If the website doesn't require authentication, my code works fine, if it does, it errors with EXC_BAD_ACCESS right after printing "credential created". I'm not releasing anything, and this code is copied straight from the documentation - any idea what's wrong?

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

  [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-08-29 13:43:16

事实证明,这与特定网站的身份验证方法有关 - 将我想要登录的网站替换为另一个网站,代码工作正常。

(我试图从 unRaid 的状态页面抓取数据,unRaid 不使用实际的网络服务器,而是使用 emhttp,所以我假设这与它有关)

Turns out it's something to do with that particular sites authentication method - replacing the website I want to log into with another website and the code works fine.

(I was trying to scrape data from unRaid's status page, unRaid doesn't use an actual webserver but emhttp so I'm assuming that has something to do with it)

失去的东西太少 2024-08-29 13:43:16

您唯一无法控制的是[challenge sender] 的返回值。尝试打印其描述以确保它非零。

The only thing you're not controlling is the return value of [challenge sender]. Try printing its description to ensure that it's non-nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文