4
votes

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

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

However I'm getting error Code=-1202 - "untrusted server certificate"

I have a self signed certificate from the server which I installed on my Mac's keychain (and verified that https requests from browser are going fine).

I don't want to ignore the certificate by sending data asynchronously and handling didReceiveAuthenticationChallenge

Should sendSynchronousRequest not work if the certificate is installed in keychain. Am I missing something?

1
can somebody please answer this.. I need to get this working!! :(dips

1 Answers

0
votes

As far as I know you can't do this with a synchronous request. At least not with NSURLConnection. You could use ASIHTTPRequest and it would look like this:

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

When speaking of 3rd party libs, AFNetworking can do this as well (in several ways) and it also has an option to work synchronously.

#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_     
#endif

Last but not least you can do it whith going low level and using CFNetworking itself (you can look at the ASIHTTPRequest code how to do this), but this would probably add more boilerplate code than using NSURLConnectionDelegate.

I also mentioned the 3rd party libs because they use different approaches to asynchronise HTTP requests. I encourage you to have a look at them, because they may better fit the reasons why you don't want to use NSURLConnectionDelegate.