ios - NSURLConnectionDelegate willSendRequestForAuthenticationChallenge 不会在 iOS 8 上被调用

标签 ios objective-c iphone nsurlconnection ios8

我的服务器提供了几种身份验证方法:NTLM 和摘要。
我的 iOS 客户端不会处理 NTLM 身份验证,因此我实现了 connection:willSendRequestForAuthenticationChallenge: 委托(delegate)来拒绝 NTLM,然后仅将正确的凭据用于摘要式身份验证质询。
到目前为止,在 iOS 7 上一切正常。

但是在 iOS 8 上,我发现了一个奇怪的行为:
connection:willSendRequestForAuthenticationChallenge: 委托(delegate)最多不会被调用 (95%)!!

我得到了这个错误:

Error: Error Domain=NSPOSIXErrorDomain Code=54 "The operation couldn’t be completed. Connection reset by peer" 
UserInfo=0x16520fb0 {_kCFStreamErrorCodeKey=54, 
NSErrorPeerAddressKey=<CFData 0x16682e40 [0x2f752440]>{length = 16, capacity = 16, bytes = 0x10020d7eac12780b0000000000000000}, 
NSErrorFailingURLKey=http://SERVER_IP:SERVER_PORT/Tunnel/Message.aspx, 
NSErrorFailingURLStringKey=http://SERVER_IP:SERVER_PORT/Tunnel/Message.aspx, 
_kCFStreamErrorDomainKey=1}

只有 5% 的时间委托(delegate)被正确调用并照常工作。

下面显示了我如何将我的请求发送到服务器并处理身份验证挑战:

- (void)postRequest
{
    NSString *IP = SERVER_IP;
    int port = SERVER_PORT;

    NSString *url = [NSString stringWithFormat:@"http://%@:%d/Tunnel/Message.aspx", IP, port];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];

    NSString *xml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetServerInfo></GetServerInfo>"];
    [request setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"%@", challenge.protectionSpace);

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest])
    {
        if ([challenge previousFailureCount] == 0)
        {
            [[challenge sender] useCredential:[NSURLCredential credentialWithUser:USERNAME
                                                                         password:PASSWORD
                                                                      persistence:NSURLCredentialPersistenceNone]
                   forAuthenticationChallenge:challenge];
        }
        else
        {
            [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
        }
    }
    else
    {
        [[challenge sender] rejectProtectionSpaceAndContinueWithChallenge:challenge];
    }
}

这些代码在 iOS 7 上工作,willSendRequestForAuthenticationChallenge 在身份验证质询期间被调用多次,但在 iOS 8 上甚至一次都没有被调用!

这可能是 iOS 8 的错误还是自 iOS 8 以来发生的变化?

最佳答案

当我将我的 iOS 7 应用程序更新到 iOS 8 时,这发生在我身上。我们使用 Oracle SOA 作为中间件,突然它停止调用委托(delegate)方法。下面在 iOS8 和 iOS7 中都对我有用。 (使用 Xcode 6.1)

  - (void) addBasicHTTPAuthenticationHeaders
    {
        NSString * wsUserName = @"userNameForWebService";
        NSString * wsPassword = @"passwordForWebService";

        NSString *authStr = [NSString stringWithFormat:@"%@:%@", wsUserName, wsPassword];
        NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
        NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]];
        [urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
    }

关于ios - NSURLConnectionDelegate willSendRequestForAuthenticationChallenge 不会在 iOS 8 上被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25566647/

相关文章:

ios - 是否有 GetMacOSStatusErrorString 和 GetMacOSStatusCommentString 的 iOS 等效项

ios - 拖入自定义分段控件时意外调用cancelTracking

ios - "Application failed codesign verification."- 在我更改 Xcode 中的一些build设置之前一直在工作

objective-c - 为什么我的 UIImageView 对象没有实时绘制?

ios - 从计划中的数组中删除适当的 CCSprite

iphone - 组件中带有标签的 UIPickerview 图像

ios - 如何解决错误 : NSURLConnectionInternalConnection _withConnectionDisconnectFromConnection

objective-c - 滚动表时向核心数据发出获取请求

iphone - 调整图像大小以适合比例

iphone - 我可以在 iTunes 中为我的应用指定不支持 IOS 5 吗?