web services - Sending post request to php webservice is not returning any value in swift 2 -
i want invoke or send request php webservice generates response in xml format, , need parse data sound link , send player.
i have wrote below code when debug there no response returned.
any suggestion?
var soapmsg = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:envelope xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:body<getsounds xmlns=\"http://test.com/soap/tv\"><language></language></getsounds></soap:body></soap:envelope>" let urlstring: string = "http://test.com/tv/soapservices.php?wsdl" var url: nsurl = nsurl(string: urlstring)! //var request: nsurlrequest = nsurlrequest(url: url) var request = nsmutableurlrequest(url: url) var msglength = string(soapmsg.characters.count) request.httpmethod = "post" request.httpbody = soapmsg.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) request.addvalue("text/xml; charset=utf-8", forhttpheaderfield: "content-type") request.addvalue(msglength, forhttpheaderfield: "content-length") request.addvalue("http://bee.myservice.com/userlogin", forhttpheaderfield: "action") var connection: nsurlconnection = nsurlconnection(request: request, delegate: self, startimmediately: true)! connection.start() print(request) var response: nsurlresponse? // var data = nsurlconnection.sendsynchronousrequest(request, returningresponse: &response, error: nil) nsdata? do{ var data = try nsurlconnection.sendsynchronousrequest(request , returningresponse: &response) if let httpresponse = response as? nshttpurlresponse { // print(<#t##items: any...##any#>)("error \(httpresponse.statuscode)") print(response) } } catch{ print("error") }
implement delegate methods,
func connection(didreceiveresponse: nsurlconnection!, didreceiveresponse response: nsurlresponse!) { // recieved new request, clear out data object self.data = nsmutabledata() } func connection(connection: nsurlconnection!, didreceivedata condata: nsdata!) { // append recieved chunk of data our data object // if connection receiving data self.data.appenddata(condata) } func connectiondidfinishloading(connection: nsurlconnection!) { // request complete, self.data should hold resulting info let responsestring: nsstring = nsstring(data: self.responsedata, encoding: nsutf8stringencoding); println(responsestring) }
here self.data var of nsmutabledata() type stores xml response. if every thing works fine, responsestring should print xml.
p.s dont forget class yourclass : nsobject, nsurlconnectiondelegate
Comments
Post a Comment