2012-07-26 54 views
1

我使用的服務器上有一個無效證書的Web服務,但我想要得到響應。 有沒有辦法做到這一點? 這裏的示例代碼:如何繼續使用不受信任的服務URL?

-(void)createHttpHeaderRequest:(serviceType)type { 
    NSMutableURLRequest * theRequest; 
    if (type==kServiceTypeTopAccessory) { 

     NSString * test = @"{\"GetVehicleInventory\": {\"ApplicationArea\": some Json object here}}}}}"; 

     NSString * final = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)test, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8); 
     NSString * sampleReq = [NSString stringWithFormat:@"https://myuntrutsedServer?XML_INPUT=%@",final]; 
     // NSString * final = [sampleReq stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

     NSLog(@"JSON:%@",sampleReq); 


     theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sampleReq]]; 
    } 
    NSLog(@"Request:%@",theRequest); 
    self.theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; 
    if (self.theConnection) { 
     NSLog(@"Service hit"); 
    } 
} 

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"Error%@",error); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 

    NSLog(@"Reached here"); 

} 

我得到這個錯誤在我didFailWithError方法:NSLocalizedDescription =證書此服務器是無效的。您可能正在連接假裝爲「209.112.58.126」的服務器,這可能會將您的機密信息置於危險之中,NSUnderlyingError = 0x6877b40「此服務器的證書無效。您可能正在連接到假裝服務器的服務器爲「209.112.58.126」,它可以把您的機密信息處於危險之中「,NSURLErrorFailingURLPeerTrustErrorKey =}

感謝,

+0

連接到這些網站是一個巨大的安全風險,除非你只有一個例外,只有這個網站,只有這個網站。如果沒有,很多人都會很痛苦,假設這是分佈式的 – JTApps 2012-07-26 17:18:06

回答

4

我固定它通過這樣做:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
} 

享受!

+0

這些提到的委託方法沒有被調用..我沒有沒有我有導入/ * NSURLConnectionDelegate * / – 2017-12-21 06:30:57

3

這是我從蘋果公司的AdvancedURLConnections提取示例代碼:

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 

    // Verify certificate: 
    SecTrustResultType trustResult; 
    OSStatus status = SecTrustEvaluate(challenge.protectionSpace.serverTrust, &trustResult); 
    BOOL trusted = (status == errSecSuccess) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified)); 

    if (trusted) { 
     [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] 
      forAuthenticationChallenge:challenge]; 
    } else { 
     if (user_wants_to_trust_invalid_certificates) { 
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] 
       forAuthenticationChallenge:challenge]; 
     } else { 
      [challenge.sender cancelAuthenticationChallenge:challenge]; 
     } 
    } 
} else { 
    [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge]; 
} 

注:作爲mbm30075正確地指出,這個代碼進入

connection:didReceiveAuthenticationChallenge: 

與iOS 5開始,你可以使用委託函數

connection:willSendRequestForAuthenticationChallenge: 

代替。您必須將安全框架添加到您的項目中才能進行編譯。

+0

重要信息從您的答案中缺失:1)此代碼連接:didReceiveAuthenticationChallenge,和2)您必須添加安全框架以您的項目並將其導入您的代碼中以使用SecTrustEvaluate。除此之外,很好的例子!謝謝! – mbm29414 2012-10-05 11:15:50

+0

@ mbm30075:感謝您的反饋。實際上,您可以在'willSendRequestForAuthenticationChallenge:'中執行此操作,它將替換iOS 5中的其他委託函數'canAuthenticateAgainstProtectionSpace:'和'didReceiveAuthenticationChallenge:'。我將相應地更新答案。 – 2012-10-05 11:23:33

+0

不知道!感謝更新。這對我來說是一個朦朧的地方,但它變得更清晰了! – mbm29414 2012-10-05 11:39:11

相關問題