Sak*_*Cho 11 iphone objective-c nsurlconnection ios ios8
我的服务器提供了几种身份验证方法:NTLM和摘要.
我的iOS客户端不会处理NTLM身份验证,因此我实现connection:willSendRequestForAuthenticationChallenge:委托拒绝NTLM,然后仅使用正确的凭据进行摘要身份验证质询.
到目前为止,iOS 7上的一切正常.
但在iOS 8,我发现了一个奇怪的现象:
在connection:willSendRequestForAuthenticationChallenge:代表不会在大部分时间(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}
Run Code Online (Sandbox Code Playgroud)
只有5%的时间正确调用代理并照常工作.
下面显示了如何将我的请求发送到服务器并处理身份验证质询:
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
这些代码在iOS 7上运行,willSendRequestForAuthenticationChallenge在身份验证挑战期间多次调用,但在iOS 8上甚至没有调用过一次!
这可能是iOS 8的错误或iOS 8以来的变化吗?
当我将 iOS 7 应用程序更新到 iOS 8 时,就发生了这种情况。我们使用 Oracle SOA 作为中间件,突然它停止调用委托方法。下面的代码在 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"];
}
Run Code Online (Sandbox Code Playgroud)