1
为了使用ssl调用Web服务,我将子类NSURLConnection。 我想过要做到同步,而不仅仅是现在的异步连接。带有ssl的同步子类NSURLConnection?
我的代码:
#import "SecureSSLConnection.h"
static NSMutableArray *sharedConnectionList = nil;
@implementation SecureSSLConnection
@synthesize request, completionBlock, internalConnection;
- (id)initWithRequest:(NSMutableURLRequest *)req
{
self = [super init];
if (self) {
self.request = req;
}
return self;
}
- (void)start
{
container = [NSMutableData new];
internalConnection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];
if (!sharedConnectionList) {
sharedConnectionList = [NSMutableArray new];
}
[sharedConnectionList addObject:self];
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[container appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (self.completionBlock) {
self.completionBlock(container, nil);
}
[sharedConnectionList removeObject:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (self.completionBlock) {
self.completionBlock(nil, error);
}
[sharedConnectionList removeObject:self];
}
#pragma mark - SSL Connection Addition
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"challenge.protectionSpace.host: %@", challenge.protectionSpace.host);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// We only trust our own domain
if ([challenge.protectionSpace.host isEqualToString:WebSiteURL]) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
我开始考虑实施:
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
为了保持同原来的委托。
一种方法是使用NSNotificationCenter,但我的目标是更容易的方法。
任何想法我应该怎么做呢?
你为什么继承NSURLConnection?只是为'sharedConnectionList'?你不需要提供你自己的代表。 – zaph
@Zaph我同意共享连接数组结构很好奇,因为子类“NSURLConnection”的概念。但我不确定为什么你反对他自己的委托实现,因为(a)比调用'sendAsynchronousRequest'要好得多, (b)他的代表实际上正在处理认证挑战。也许我不明白你想对代表说什么。 – Rob
我并不是反对代表的实现,而是指出子类化不是实现代表的必要条件。 – zaph