SendSynchronousRequest using a self-signed certificate installed on Mac OS X keychain for Mac

I am sending a https request from Mac OS X (not iphone) to the web server synchronously using

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

However, I get the error Code = -1202 - "untrusted server certificate"

I have the signed certificate from the server itself, which I installed on my Mac keychain (and checked that the https requests from the browser are going well).

I do not want to ignore the certificate by sending data asynchronously and processing didReceiveAuthenticationChallenge

If sendSynchronousRequest does not work, if the certificate is installed in the keychain. Did I miss something?

+4
source share
1 answer

As far as I know, you cannot do this with a synchronous request. At least not with NSURLConnection. You can use ASIHTTPRequest and it will look like this:

 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setValidatesSecureCertificate:NO] [request startSynchronous]; 

When it comes to third-party libraries, AFNetworking can do this (in several ways) and it also has the ability to work synchronously.

 #ifdef DEBUG #define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ #endif 

Last but not least, you can do it at a low level and using CFNetworking itself (you can see the ASIHTTPRequest code how to do it), but it will probably add more boilerplate code than using NSURLConnectionDelegate.

I also mentioned third-party libraries because they use different approaches to asynchronizing HTTP requests. I urge you to take a look at them because they can better match the reasons why you do not want to use NSURLConnectionDelegate.

0
source

Source: https://habr.com/ru/post/1386620/


All Articles