2

Briefly my problem is i have tried to connect to https url using NSURLConnection class and it s delegate methods.

I was getting "An SSL Error has occured and a secure connection to the server could not be made" error

So i implemented this solution.but no luck.Those delegate methods specified in the solution never gets called.

only

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
method

gets called with the above error

So i shifted to use CFNetwork API to connect to https url with a self signed certificate

And here is my code.I took it from here and CFNetwork programming guide from apple

CFReadStreamRef readStream;
            CFHTTPMessageRef request;

            CFStringRef requestMessage = (CFStringRef)postparameters;

            request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"),
                                                 (CFURLRef) postURL, kCFHTTPVersion1_1);


            CFHTTPMessageSetBody(request, (CFDataRef) requestMessage);
            readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
            CFRelease(request);

            NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
                                      [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
                                      kCFNull,kCFStreamSSLPeerName,
                                      nil];

            CFReadStreamSetProperty((CFReadStreamRef)readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

            CFReadStreamOpen(readStream);

            /* Add to the run loop */
            CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),
                                            kCFRunLoopCommonModes);

            if (!CFReadStreamOpen(readStream)) {
            DLog(@"method called");
            [_progressAlert dismissWithClickedButtonIndex:0 animated:YES];
            CFReadStreamSetClient(readStream, 0, NULL, NULL);
            CFReadStreamUnscheduleFromRunLoop(readStream,
                                              CFRunLoopGetCurrent(),
                                              kCFRunLoopCommonModes);
            CFRelease(readStream);
        }
        else {
        CFHTTPMessageRef myResponse =
        (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream,
                                                   kCFStreamPropertyHTTPResponseHeader);
        //You can get the complete status line from the response message by calling the function CFHTTPMessageCopyResponseStatusLine:
        CFStringRef myStatusLine = CFHTTPMessageCopyResponseStatusLine(myResponse);
        DLog(@"status: %@", (NSString *)myStatusLine);

        UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myResponse);
        NSLog(@"error :%lu", myErrCode);
        }

The output i see in the console is

2011-11-30 21:02:07.720 [3800:f803][Line 180] method called

What i need to add to my above code to make it work.

I guess it is something related to authentication.

Please help me with some sample code.

Update: I updated my code above and the response.My !CFReadStreamOpen(readStream) method returns true and the log gets printed

Community
  • 1
  • 1
Sreeram
  • 3,160
  • 6
  • 33
  • 44

0 Answers0