NT_iOS note-how does NSURLConnection change http to https?

Source: Internet
Author: User

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];        }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.