Consuming SOAP web service with Swift

I am expecting XML SOAP response from w3schools.com example ( http://wwww.w3schools.com/webservices/tempconvert.asmx ), but what I get is a pure html urlString. I am following the example from http://webindream.com/soap-with-swift/

thanks for the help

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

    var mutableData: NSMutableData = NSMutableData.alloc()
    var currentElementName: NSString = ""

    @IBOutlet weak var txtCelsius: UITextField!
    @IBOutlet weak var txtFahrenheit: UITextField!

    @IBAction func btnConvert(sender: AnyObject) {

        var celsius = txtCelsius.text

        var soapMessage = "<?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><CelsiusToFahrenheit xmlns='http://www.w3schools.com/webservices/'><Celsius>\(celsius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"
        var urlString = "http://wwww.w3schools.com/webservices/tempconvert.asmx"
        var url = NSURL(string: urlString)
        var theRequest = NSMutableURLRequest(URL: url!)
        var msgLength = String(countElements(soapMessage))

        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

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

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

    }

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

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

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

    func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) {
        currentElementName = elementName
    }

    func parser(parser: NSXMLParser!, foundCharacters string: String!) {
        println(string)
        if currentElementName == "CelsiusToFahrenheitResult" {
            txtFahrenheit.text = string
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("View did Load")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

      

change *

I am getting clean html urlString ...

<head><link rel="alternate" type="text/xml" href="/webservices/tempconvert.asmx?disco" />

<style type="text/css">

    BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; }
    #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; }
    A:link { color: #336699; font-weight: bold; text-decoration: underline; }
    A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; }
    A:active { color: #336699; font-weight: bold; text-decoration: underline; }
    A:hover { color: cc3300; font-weight: bold; text-decoration: underline; }
    P { color: #000000; margin-top: 0px; margin-bottom: 12px; font-family: Verdana; }
    pre { background-color: #e5e5cc; padding: 5px; font-family: Courier New; font-size: x-small; margin-top: -5px; border: 1px #f0f0e0 solid; }
    td { color: #000000; font-family: Verdana; font-size: .7em; }
    h2 { font-size: 1.5em; font-weight: bold; margin-top: 25px; margin-bottom: 10px; border-top: 1px solid #003366; margin-left: -15px; color: #003366; }
    h3 { font-size: 1.1em; color: #000000; margin-left: -15px; margin-top: 10px; margin-bottom: 10px; }
    ul { margin-top: 10px; margin-left: 20px; }
    ol { margin-top: 10px; margin-left: 20px; }
    li { margin-top: 10px; color: #000000; }
    font.value { color: darkblue; font: bold; }
    font.key { color: darkgreen; font: bold; }
    font.error { color: darkred; font: bold; }
    .heading1 { color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal; background-color: #003366; margin-top: 0px; margin-bottom: 0px; margin-left: -30px; padding-top: 10px; padding-bottom: 3px; padding-left: 15px; width: 105%; }
    .button { background-color: #dcdcdc; font-family: Verdana; font-size: 1em; border-top: #cccccc 1px solid; border-bottom: #666666 1px solid; border-left: #cccccc 1px solid; border-right: #666666 1px solid; }
    .frmheader { color: #000000; background: #dcdcdc; font-family: Verdana; font-size: .7em; font-weight: normal; border-bottom: 1px solid #dcdcdc; padding-top: 2px; padding-bottom: 2px; }
    .frmtext { font-family: Verdana; font-size: .7em; margin-top: 8px; margin-bottom: 0px; margin-left: 32px; }
    .frmInput { font-family: Verdana; font-size: 1em; }
    .intro { margin-left: -15px; }

</style>

<title>
TempConvert Web Service

      

<div id="content">

  <p class="heading1">TempConvert</p><br>



  <span>

      <p class="intro">The following operations are supported.  For a formal definition, please review the <a href="tempconvert.asmx?WSDL">Service Description</a>. </p>


          <ul>

          <li>
            <a href="tempconvert.asmx?op=CelsiusToFahrenheit">CelsiusToFahrenheit</a>


          </li>
          <p>

          <li>
            <a href="tempconvert.asmx?op=FahrenheitToCelsius">FahrenheitToCelsius</a>


          </li>
          <p>

          </ul>

  </span>




<span>

</span>

      

+3


source to share


2 answers


curl -s --header "content-type: text/xml" -d @request.xml http://www.w3school <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"><CelsiusToFahrenheitResult>104</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>

      



+2


source


All you have to do is add? wsdl at the end of the url http://www.w3schools.com/webservices/tempconvert.asmx?wsdl



PS: You can also create Swift Soap Client for this WSDL at: http://wsdl2swift.com (Disclaimer: I'm the author)

0


source







All Articles