NT_iOS note-how does NSURLConnection change http to https?
I have been using NSURLConnection to request the HTTP interface. Now I plan to Use HTTPS for security considerations.
So how can we modify it?
1. No certificate verification required. ps: This is what we use.
1.1 directly change HTTP to HTTPS;
1.2 confirm "Security. framework"
1.3 The modification is complete. You can directly request the modification.
2. Certificate verification required
Others and 1 are the same, but the following method must be added.
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ static CFArrayRef certs; if (!certs) { NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"srca" ofType:@"cer"]]; SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(certData)); const void *array[1] = { rootcert }; certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks); CFRelease(rootcert); // for completeness, really does not matter } SecTrustRef trust = [[challenge protectionSpace] serverTrust]; int err; SecTrustResultType trustResult = 0; err = SecTrustSetAnchorCertificates(trust, certs); if (err == noErr) { err = SecTrustEvaluate(trust,&trustResult); } CFRelease(trust); BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed)||(trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified)); if (trusted) { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; }else{ [challenge.sender cancelAuthenticationChallenge:challenge]; }}