iPhone上的同步SSL证书处理 [英] Synchonous SSL certificate handling on iPhone
问题描述
我想知道是否有人可以帮助我了解如何向同步添加SSL证书处理连接到https服务.
I was wondering if anyone can help me understand how to add SSL certificate handling to synchronous connections to a https service.
我知道如何通过异步连接来实现此目的,而不是同步.
I know how to do this with asynchronous connections but not synchronous.
NSString *URLpath = @"https://mydomain.com/";
NSURL *myURL = [[NSURL alloc] initWithString:URLpath];
NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[myURL release];
[myURLRequest setHTTPMethod:@"POST"];
NSString *httpBodystr = @"setting1=1";
[myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* myURLResponse;
NSError* myError;
NSData* myDataResult = [NSURLConnection sendSynchronousRequest:myURLRequest returningResponse:&myURLResponse error:&myError];
//I guess I am meant to put some SSL handling code here
谢谢.
推荐答案
使用静态sendSynchronousRequest函数不是可行的方法,但是我找到了另一种方法.
Using the static sendSynchronousRequest function is not posible, but i found an alternative.
首先像这样的 NSURLConnectionDataDelegate 对象
FailCertificateDelegate.h
@interface FailCertificateDelegate : NSObject <NSURLConnectionDataDelegate>
@property(atomic,retain)NSCondition *downloaded;
@property(nonatomic,retain)NSData *dataDownloaded;
-(NSData *)getData;
@end
FailCertificateDelegate.m
#import "FailCertificateDelegate.h"
@implementation FailCertificateDelegate
@synthesize dataDownloaded,downloaded;
-(id)init{
self = [super init];
if (self){
dataDownloaded=nil;
downloaded=[[NSCondition alloc] init];
}
return self;
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *)protectionSpace {
NSLog(@"canAuthenticateAgainstProtectionSpace:");
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge {
NSLog(@"didReceiveAuthenticationChallenge:");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[downloaded signal];
[downloaded unlock];
self.hasFinnishLoading = YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[dataDownloaded appendData:data];
[downloaded lock];
}
-(NSData *)getData{
if (!self.hasFinnishLoading){
[downloaded lock];
[downloaded wait];
[downloaded unlock];
}
return dataDownloaded;
}
@end
并使用它
FailCertificateDelegate *fcd=[[FailCertificateDelegate alloc] init];
NSURLConnection *c=[[NSURLConnection alloc] initWithRequest:request delegate:fcd startImmediately:NO];
[c setDelegateQueue:[[NSOperationQueue alloc] init]];
[c start];
NSData *d=[fcd getData];
现在您将拥有使用nsurlconnection异步使用的所有好处以及简单的同步连接的好处,直到您在委托上下载所有数据之前,线程都将被阻塞,但是您可以对其进行改进,在FailCertificateDelegate类上添加一些错误控制
Now you will have all benefits of have an async use of nsurlconnection and benefits of a simple sync connection, the thread will be blocked until you download all data on the delegate, but you could improve it adding some error control on FailCertificateDelegate class
修复大数据.根据Nikolay DS的评论.非常感谢
fix for big data. based on Nikolay DS comment. Thanks a lot
这篇关于iPhone上的同步SSL证书处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!