Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"untrusted server certificate" on iPhone

I am writing an iPhone application and I would like to connect to a 'HTTPS' server to get some information. However, I get the error in the console which is

NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork Code=-1202 UserInfo=0x3e95cf0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk."; }

How can I trust the certificate and get the http status code 200

The following is my code.

    NSMutableURLRequest *request_get2 = [[[NSMutableURLRequest alloc] init] autorelease];
    [request_get2 setURL:[NSURL URLWithString:@"https://www.example.com"]]; 
    [request_get2 setHTTPMethod:@"GET"];
    [request_get2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    //[request_get2 setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request_get2 setValue:@"https://www.example.com" forHTTPHeaderField:@"Referer"];
    [request_get2 setHTTPShouldHandleCookies:YES];
    // cookiesString is in format "cookieName=cookieValue;"
    [request_get2 setValue: (NSString *) cookiesString forHTTPHeaderField:@"Cookie"];


    // doGet - response
    NSHTTPURLResponse *response_get2 = nil;  // it change from 'NSURLRespaonse' to 'NSHTTPURLResponse'
    NSError *error_get2 = nil;
    NSData *responseData_get2 = [NSURLConnection sendSynchronousRequest:request_get2 returningResponse:&response_get2 error:&error_get2];
    NSString *data_get2 = [[NSString alloc]initWithData:responseData_get2 encoding:NSUTF8StringEncoding];

    if (!error_get2) {
        NSString *responseURL_get2 = [[response_get2 URL] absoluteString];           // null value
        NSString *responseTextEncodingName_get2 = [response_get2 textEncodingName];  // null value
        NSString *responseMIMEType_get2 = [response_get2 MIMEType];                  // null value
        NSUInteger *responseStatusCode_get2 = [response_get2 statusCode]; //[responseStatusCode intValue]; // the status code is 0
    }
    else {
        NSLog(@"\nsomething went wrong: %@\n", [error_get2 userInfo]); // got the error in here
    }
like image 696
Questions Avatar asked Jul 05 '10 08:07

Questions


People also ask

What does Untrusted server certificate mean?

An Untrusted Server Certificate error indicates that the certificate (one of the elements proving that data is only going to trusted locations online) that the Zoom application is seeing is ​not​ the certificate that was expected.

How do I fix Untrusted certificate Zoom?

Resolve Untrusted Certificate If correcting the time doesn't help, typically, your IT Admin(s) would need to procure and install a signed and trusted certificate online. They would then apply this onto your device—or into the system they're using—that is not passing the expected certificate on to you.


1 Answers

I think that adding an untrusted certificate is (at least in the simulator) not possible, but you can tell your NSURLConnection Delegate to accept self signed certificates (or generally untrusted ones)

The following link helped me solve the issue!

How to use NSURLConnection to connect with SSL for an untrusted cert?

like image 134
samsam Avatar answered Oct 03 '22 15:10

samsam