在我的应用中,我正在尝试连接到IIS服务器以访问某些服务器。 当我在iOS 4或更早版本中运行应用程序时,连接正常,但是当我在iOS 5中运行时,连接会返回此错误:
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x6b601d0 {NSUnderlyingError=0x6b5fe20 "The operation couldn’t be completed. (OSStatus error -9836.)", NSLocalizedDescription=A connection failure occurred}
这是我打电话的代码:
self.currentRequest = [ASIHTTPRequest requestWithURL:url];
[self.currentRequest setValidatesSecureCertificate:NO];
[self.currentRequest setDelegate:self];
[self.currentRequest setUsername:credentials.login];
[self.currentRequest setPassword:credentials.password];
[self.currentRequest setDomain:@"CORP"];
[self.currentRequest setUseHTTPVersionOne:YES];
[self.currentRequest setTimeOutSeconds:120];
[self.currentRequest startAsynchronous];
有人知道为什么会这样吗?
答案 0 :(得分:5)
-9836是一个错误的协议错误。这可能是由于iOS 5的TLS实现升级到TLS 1.2。如果服务器不支持TLS 1.2,它可能会终止握手并且无法降级到支持的协议级别。
在ASIHTTPRequest.m中,在 - (void)startRequest
中更改为以下内容//
// Handle SSL certificate settings
//
if([[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) {
NSDictionary *sslProperties = [[NSDictionary alloc] initWithObjectsAndKeys:
@"kCFStreamSocketSecurityLevelTLSv1_0SSLv3", (NSString *)kCFStreamSSLLevel,
nil];
CFReadStreamSetProperty((CFReadStreamRef)[self readStream],
kCFStreamPropertySSLSettings,
(CFTypeRef)sslProperties);
// Tell CFNetwork not to validate SSL certificates
if (![self validatesSecureCertificate]) {
// see: http://iphonedevelopment.blogspot.com/2010/05/nsstream-tcp-and-ssl.html
NSDictionary *sslProperties = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
kCFNull,kCFStreamSSLPeerName,
@"kCFStreamSocketSecurityLevelTLSv1_0SSLv3", (NSString *)kCFStreamSSLLevel,
nil];
CFReadStreamSetProperty((CFReadStreamRef)[self readStream],
kCFStreamPropertySSLSettings,
(CFTypeRef)sslProperties);
}
// Tell CFNetwork to use a client certificate
if (clientCertificateIdentity) {
NSMutableDictionary *sslProperties = [NSMutableDictionary dictionaryWithCapacity:2];
NSMutableArray *certificates = [NSMutableArray arrayWithCapacity:[clientCertificates count]+1];
// The first object in the array is our SecIdentityRef
[certificates addObject:(id)clientCertificateIdentity];
// If we've added any additional certificates, add them too
for (id cert in clientCertificates) {
[certificates addObject:cert];
}
[sslProperties setObject:certificates forKey:(NSString *)kCFStreamSSLCertificates];
[sslProperties setObject:@"kCFStreamSocketSecurityLevelTLSv1_0SSLv3" forKey:(NSString *)kCFStreamSSLLevel];
CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySSLSettings, sslProperties);
}
}