Localhost: http vs https in URL::URL()?

I’m noticing some errors when I try to use http in the url string compared to https. i’m trying to develop my webserver code for my app that is running on localhost before it gets deployed to a server with a real SSL certificate.

These are the error messages when I try to use url(“http://localhost/script.php”);

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x600000045790 {Error Domain=kCFErrorDomainCFNetwork Code=-1022

and when i try to use url(“https://localhost/script.php”);

 NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrust 0x101c122d0 [0x7fff7c716390]>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey=(
    "<SecCertificate 0x101c153d0 [0x7fff7c716390]>"
), NSUnderlyingError=0x61800005b300 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." 

my code looks like this:

CurlyURL curly;
bool result = curly.fetch("http://localhost/script.php");
//...
bool CurlyURL::fetch(String url) {
		//populates _url, makes sure options is set
		//starts thread.  run() does the actual calling
		URL connectURL = URL(url).withPOSTData(_options);
		//URL connectURL = URL( url ).withParameters(_keyOptions );
		DBG( "url: " + connectURL.toString(true) );
		//mContent = connectURL.readEntireTextStream();

        /*
         (const bool usePostCommand,
         OpenStreamProgressCallback* const progressCallback,
         void* const progressCallbackContext,
         String headers,
         const int timeOutMs,
         StringPairArray* const responseHeaders,
         int* statusCode,
         const int numRedirectsToFollow,
         String httpRequestCmd) const
         */
		int statusCode = 0;
        ScopedPointer<InputStream> stream(connectURL.createInputStream(true, //bool usePostCommand
                                                                       nullptr,    //progressCallback
                                                                       nullptr,    //progressCallbackContext
                                                                       String(),   //headers
                                                                       10000,  //timeoutMS
                                                                       nullptr,    //respondHeaders
                                                                       &statusCode)//statusCode
			);
		if (stream != nullptr) {
			mContent = stream->readEntireStreamAsString();
			return true;
		}
		if (statusCode != 0 && statusCode != 200) {
			mContent = "Failed to connect, status code = " + String(statusCode);
			return false;
		}
		DBG( "result: " + mContent  );
		return false;

	}'

as I stated earlier, it works perfectly fine when the server i’m trying to remotely connect to has a legitimate SSL certificate installed.

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Paste that in CustomPList on the Introjucer Xcode Export Target, resave your project, recompile and it should work.

1 Like

Thanks. Was that dict snippet in the right order?

This didn’t work for me. I ended up going into the info-app.plist in xcode directly and adding the keys. I’m not sure why it didn’t work.