我试图让上述三个工作正常,并且有些东西没有点击。具体来说,身份验证请求在出现时不会被触发。据我在这里读什么,相关作品是:ios https和基本身份验证和发布请求
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
NSLog(@"protection space");
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"auth challenge");
NSInteger count = [challenge previousFailureCount];
if (count > 0)
{
NSLog(@"count > 0");
NSObject<ServiceDelegate> *delegate = [currentRequest delegate];
[[challenge sender] cancelAuthenticationChallenge:challenge];
if ([delegate respondsToSelector:@selector(wrapperHasBadCredentials:)])
{
[delegate rbService:self wrapperHasBadCredentials:self];
}
return;
}
NSArray *trustedHosts = [[NSArray alloc] initWithObjects:@"myserver", nil];
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSLog(@"server trust");
if ([trustedHosts containsObject:challenge.protectionSpace.host])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else
{
NSLog(@"credentials");
NSURLCredential* credential = [[[NSURLCredential alloc] initWithUser:@"xxx" password:@"xxx" persistence:NSURLCredentialPersistenceForSession] autorelease];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
}
目标服务器设置为两个URL,两者都提示使用基本身份验证用户名/密码的一个HTTPS和HTTP一个,。我已经使用Firefox检查了目标服务器,并且一切似乎都按照广告的方式工作。目标服务器使用不受信任的证书,但我认为我已经在代码中处理了这个问题。也许不会。
在应用程序中的特定行为:
当使用HTTP日志读取:
日志 - 保护空间
(然后返回401代码)
当使用HTTPS:
日志 - 保护空间
日志 - 授权挑战
日志 - 服务器信任
日志 - 保护空间
(然后返回一个401码)
在第一种情况下,它到达canAuthenticate ......部分,退货,但当时并没有挑战的认证,并返回一个401响应。
在第二个例子中,它做了所有这些,实际上是挑战,然后再次进入canAuthenticate ...部分,返回并返回401响应。
请记住,该请求是POST请求,包含标头和HTTPBody。认证不包括在标题中(我宁愿不这样做),但如果没有其他解决方案,我会尝试这样的事情。
一如既往,非常感谢您的帮助。它是无价的。
你可以如此善待张贴你的soloution? – Bjarke
我的歉意 - 我没有看到你的这个要求。我关闭了这一段时间,但总的来说,我所做的是将头部附加到请求“手动” – jb44