Using NSURLConnection with a valid SSL certificate in Swift

I am relatively new to iOS development. So please bear with me and goodbye anything.

I am trying to get data from a SOAP web service using NSURLConnection and it works fine.

However, as soon as I change the URL from http to https, I no longer receive any data, and I don't have an error or anything like that, I would expect.

The https certificate is a valid certificate from godaddy, and it is correctly configured and correctly looks at all browsers, etc., therefore, in principle, someone can point me in the right direction why nothing happens when switching from http to https .... ..

The code is in Swift and looks like this:

    // Create the SOAP Message to send
    var soapMessage = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://tempuri.org/'><SOAP-ENV:Body><ns1:get_Test/></SOAP-ENV:Body></SOAP-ENV:Envelope>"

    NSLog("soapMessage generated is: %@", soapMessage)

    // Soap URL Works
    var urlString = "http://api.domain.com/testservice.svc"

    //Below https URL does not work
    //var urlString = "https://api.domain.com/testservice.svc"

    var url = NSURL(string: urlString)
    var theRequest = NSMutableURLRequest(URL: url!)

    //Get Length of request
    var msgLength = String(countElements(soapMessage))

    // POST Header values
    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.addValue("http://tempuri.org/IService/get_Test", forHTTPHeaderField: "SoapAction")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    NSLog("Request is: %@", theRequest.allHTTPHeaderFields!)

    var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
    connection?.start()

    if (connection == true) {
        var mutableData : Void = NSMutableData.initialize()
    }

Additional NSURLConnection code:

// NSURLConnectionDelegate
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    mutableData.length = 0;
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
    mutableData.appendData(data)
}


func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    NSLog("Error with Soap call: %@", error)
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
    var xmlParser = NSXMLParser(data: mutableData)
    xmlParser.delegate = self
    xmlParser.parse()
    xmlParser.shouldResolveExternalEntities = true
}
// NSURLConnectionDelegate

, didStartElement, didEndElement, foundCharacters, parserDidEndDocument ..

NSURLConnection, , . .

 func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
     NSLog("am here")
    return protectionSpace?.authenticationMethod == NSURLAuthenticationMethodServerTrust
}

func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge?)
{
     NSLog("am here 2")
    if challenge?.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
    {
        if challenge?.protectionSpace.host == "api.domain.com"
        {
            NSLog("yep")
            let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust)
            challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge!)
        }
    }
    challenge?.sender.continueWithoutCredentialForAuthenticationChallenge(challenge!)
}

, "yep", , , , , https-url.

, , https, , https, http?

- , !

Dave

:

ok :

    var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: false)
    connection?.start()

    if (connection == true) {
        var mutableData : Void = NSMutableData.initialize()
    } else {
        NSLog("Error with connection, details: %@", connection!)
    }

, " ", SSL!

, didreceiveresponse :

func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    mutableData.length = 0;
    var httpresponse = response as? NSHTTPURLResponse
    println("status \(httpresponse?.statusCode)")
    println("headers \(httpresponse?.allHeaderFields)")
}

, - 404, , - -!

, , , . - , ?

+4
1

- , - - SVC :

     <bindings>
        <basicHttpBinding>
            <binding>
                <security mode="Transport">
                    <transport clientCredentialType="None"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="Service.Service">
            <endpoint address="" binding="basicHttpBinding" contract="Service.IService" />
        </service>
    </services>

, ! , !

+1

All Articles