2012-06-20 81 views
0

目前我有:如何忽略Objective-C中的「無效」SSL證書?

NSArray* array = [NSArray arrayWithObjects:@"auth.login",@"username",@"password", nil]; 
NSData* packed_array = [array messagePack]; 

NSURL* url = [NSURL URLWithString:@"https://192.168.1.149:3790/api/1.0"]; 
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 

//Previously the below had changed by hostname. 
//[request setValue:@"RPC Server" forHTTPHeaderField:@"Host"]; 

[request setValue:@"binary/message-pack" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d",[packed_array length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:packed_array]; 

NSURLResponse *response; 
NSError *error; 
responseData = [NSMutableData dataWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]]; 

NSLog(@"response data: %@",[responseData messagePackParse]); 
NSLog(@"error: %@",error); 
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
NSLog(@"called canAuthenticateAgainstProtectionSpace"); 
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
NSLog(@"called didReceiveAuthenticationChallenge"); 
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
} 

它返回"Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid"…

我應該如何從this question獲取數據回貫徹答案

+1

DUP http://stackoverflow.com/questions/933331/how-to-use-nsurlconnection-to-connect-with-東西ssl-for-un-untrusted-cert – hamstergene

+0

您是否忽略閱讀該問題? :^ \我無法實現該答案中的代碼。 – erran

+0

您尚未發佈實例化NSURLConnection的代碼。你是否正確地將它委託給實現這兩種方法的對象? – Taum

回答

0

我想通了這個問題(一個或多個)。首先,我修復了I was using the delegate的方式,然後我改變了主機,我編輯了代碼以刪除主機的改變並對它進行了評論我仍然不認爲我的問題是重複的...