ios https和基本身份验证和post请求

ios https和基本身份验证和post请求,ios,post,https,basic-authentication,Ios,Post,Https,Basic Authentication,我试图让上面三个正常工作,但有些东西没有点击。具体地说,身份验证请求似乎应该被触发,但却没有被触发。根据我在这里读到的内容,相关条款如下: - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"protection space"); return [protect

我试图让上面三个正常工作,但有些东西没有点击。具体地说,身份验证请求似乎应该被触发,但却没有被触发。根据我在这里读到的内容,相关条款如下:

- (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];
    }

}
-(BOOL)连接:(NSURLConnection*)连接可以针对protectionSpace进行身份验证:(NSURLProtectionSpace*)protectionSpace
{
NSLog(“保护空间”);
return[protectionSpace.authenticationMethod IseQualString:NSURLAuthenticationMethodServerTrust];
}
-(void)连接:(NSURLConnection*)连接未收到身份验证质询:(NSURLConficationChallenge*)质询
{
NSLog(@“认证挑战”);
NSInteger计数=[challenge previousFailureCount];
如果(计数>0)
{
NSLog(@“计数>0”);
NSObject*delegate=[currentRequest delegate];
[[challenge sender]cancelAuthenticationChallenge:challenge];
if([delegate respondsToSelector:@selector(wrapperHasBadCredentials:)]))
{
[委托rbService:self wrapperHasBadCredentials:self];
}
返回;
}
NSArray*trustedHosts=[[NSArray alloc]initWithObjects:@“myserver”,nil];
if([challenge.protectionSpace.authenticationMethod IsequalString:nsurAuthenticationMethodServerTrust])
{
NSLog(@“服务器信任”);
if([trustedHosts包含对象:challenge.protectionSpace.host])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]forAuthenticationChallenge:challenge];
}
[challenge.sender继续未经认证的身份验证challenge:challenge];
}
其他的
{
NSLog(“凭证”);
NSURLCredential*credential=[[NSURLCredential alloc]initWithUser:@“xxx”密码:@“xxx”持久性:NSURLCredentialPersistenceForSession]自动释放];
[[challenge sender]使用凭据:验证的凭据挑战:挑战];
}
}
目标服务器设置了两个URL,一个HTTPS和一个HTTP,这两个URL都使用基本身份验证提示输入用户名/密码。我已经使用firefox检查了目标服务器,一切都像广告中所说的那样正常。目标服务器使用不受信任的证书,但我认为我已经在代码中解决了这个问题。也许不是

应用程序中的特定行为:

使用HTTP时,日志显示:
日志-保护空间
(然后返回401代码)

使用HTTPS时:
日志-保护空间
日志-身份验证挑战
日志-服务器信任
日志-保护空间
(然后返回401代码)

在第一个例子中,它到达加拿大认证中心。。。节,返回,但随后不质询身份验证,并返回401响应

在第二个例子中,它做了所有这些,实际上是挑战,然后去加拿大认证。。。再次返回,并返回401响应

请记住,该请求是一个POST请求,包含头和HTTPBody。身份验证不包含在头文件中(我宁愿不这样做),但是如果没有其他解决方案,我会尝试这样做


一如既往,非常感谢你的帮助。这是无价的。

这里的答案似乎必须是在POST请求的同时发送身份验证。我在想,质询/响应过程会以某种方式自行将这些标题添加到请求中,但显然不是。

您能帮我发布您的解决方案吗?很抱歉,我没有看到您的请求。我在前一段时间结束了这个问题,但一般来说,我所做的是“手工”将标题附加到请求中