Moving from NSURLConnection to NSURLSession for SOAP POST in Swift -


i trying move on nsurlconnection on nsurlsession soap post, seem have issues nsurlsessiondatadelegate.

here old code in nsurlconnection works fine:

let 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_countries/></soap-env:body></soap-env:envelope>"     print("soap packet \(soapmessage)")      let urlstring = "https://example.com/service.svc"     let url = nsurl(string: urlstring)     let therequest = nsmutableurlrequest(url: url!)     let msglength = string(soapmessage.characters.count)      therequest.addvalue("text/xml; charset=utf-8", forhttpheaderfield: "content-type")     therequest.addvalue(msglength, forhttpheaderfield: "content-length")     therequest.addvalue("http://tempuri.org/iservice/get_countries", forhttpheaderfield: "soapaction")     therequest.httpmethod = "post"     therequest.httpbody = soapmessage.datausingencoding(nsutf8stringencoding, allowlossyconversion: false)     print("request \(therequest.allhttpheaderfields!)")      let connection = nsurlconnection(request: therequest, delegate: self, startimmediately: false)     connection?.start() 

this code uses nsurlconnectiondelegate, , works fine follows:

func connection(connection: nsurlconnection!, didreceiveresponse response: nsurlresponse!) {     mutabledata.length = 0;     let httpresponse = response as? nshttpurlresponse     print("status \(httpresponse?.statuscode)")     //print("headers \(httpresponse?.allheaderfields)") }  func connection(connection: nsurlconnection!, didreceivedata data: nsdata!) {     mutabledata.appenddata(data) }   func connection(connection: nsurlconnection, didfailwitherror error: nserror) {     nslog("error soap call: %@", error)  }  func connectiondidfinishloading(connection: nsurlconnection!) {     let xmlparser = nsxmlparser(data: mutabledata)     xmlparser.delegate = self     xmlparser.parse()     xmlparser.shouldresolveexternalentities = true }  func connection(connection: nsurlconnection, willsendrequestforauthenticationchallenge challenge: nsurlauthenticationchallenge) {     if challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust && challenge.protectionspace.host == "example.com" {         nslog("yep")         let credential = nsurlcredential(trust: challenge.protectionspace.servertrust!)         challenge.sender!.usecredential(credential, forauthenticationchallenge: challenge)     } else {         nslog("nope")         challenge.sender!.performdefaulthandlingforauthenticationchallenge!(challenge)     } } 

so code works fine, , reference can see have done in past, , fact api work! however, if move on using nsurlsession , nsurlsessiondatadelegate instead cannot working correctly.

so here new code:

let 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_countries/></soap-env:body></soap-env:envelope>"     print("soap packet \(soapmessage)")      let urlstring = "https://example.com/service.svc"     let url = nsurl(string: urlstring)     let therequest = nsmutableurlrequest(url: url!)     let msglength = string(soapmessage.characters.count)      therequest.addvalue("text/xml; charset=utf-8", forhttpheaderfield: "content-type")     therequest.addvalue(msglength, forhttpheaderfield: "content-length")     therequest.addvalue("http://tempuri.org/iservice/get_countries", forhttpheaderfield: "soapaction")     therequest.httpmethod = "post"     therequest.httpbody = soapmessage.datausingencoding(nsutf8stringencoding, allowlossyconversion: false)     print("request \(therequest.allhttpheaderfields!)")  let config = nsurlsessionconfiguration.defaultsessionconfiguration() let session = nsurlsession(configuration:config, delegate: self, delegatequeue: nsoperationqueue.mainqueue()) let task = session.datataskwithrequest(therequest) task.resume() 

my delegates using nsurlsessiondelegate, nsurlsessiondatadelegate:

func urlsession(session: nsurlsession, didreceivechallenge challenge: nsurlauthenticationchallenge, completionhandler: (nsurlsessionauthchallengedisposition, nsurlcredential?) -> void) {      print("am in nsurlsessiondelegate didreceivechallenge")      if challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust && challenge.protectionspace.host == "example.com" {         nslog("yep authorised")         let credential = nsurlcredential(trust: challenge.protectionspace.servertrust!)         challenge.sender!.usecredential(credential, forauthenticationchallenge: challenge)     } else {         nslog("nope")         challenge.sender!.performdefaulthandlingforauthenticationchallenge!(challenge)     }  } func urlsessiondidfinisheventsforbackgroundurlsession(session: nsurlsession) {     print("am in urlsessiondidfinisheventsforbackgroundurlsession")     let xmlparser = nsxmlparser(data: mutabledata)     xmlparser.delegate = self     xmlparser.parse()     xmlparser.shouldresolveexternalentities = true } func urlsession(session: nsurlsession, didbecomeinvalidwitherror error: nserror?) {     print("error of \(error)") }  func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceiveresponse response: nsurlresponse, completionhandler: (nsurlsessionresponsedisposition) -> void) {     print("am in didreceiveresponse")     mutabledata.length = 0 }   func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceivedata data: nsdata) {     print("am in didreceivedata")     mutabledata.appenddata(data) }  func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?) {     print("error of \(error)") } 

so, when run code, output:

"am in nsurlsessiondelegate didreceivechallenge" "yep authorised"

so it's getting didreceivechallenge fine, , appears authorising https secure certificate fine, nothing further happens, doesn't else, i'd expect go didreceiveresponse didreceivedata, nothing further happens @ all.

so stuck, of course continue , use nsurlconnection works fine, i'd understand nsurlsession, , particulary going wrong. if can great.

thanks

in case else has same issue, sorted out. issue was not using completionhandler in didreceivechallenge , didreceiveresponse delegates


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -