gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:53:09 25 4
gpt4 key购买 nike

我的服务器提供了几种身份验证方法: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com