使用Swift消费SOAP Web服务

3
我希望从w3schools.com (http://wwww.w3schools.com/webservices/tempconvert.asmx) 获取一个XML SOAP响应,但我得到的只是urlString的纯html。我正在遵循来自http://webindream.com/soap-with-swift/的示例。感谢您的帮助。
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.
    }

}

编辑*

我收到了 urlString 的纯 HTML...

<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>


修改后的问题,这是URL字符串的纯HTML。 - j.con
http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit 刚才遇到了同样的问题,它只是另一个页面的纯HTML。 - j.con
"content-type: application/soap+xml" 尝试使用 "content-type: text/xml" - j.con
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="" rel = "nofollow noreferrer">http://www.w3.org/2001/XMLSchema">soap:Body<CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"><CelsiusToFahrenheitResult>104</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope> - Rudi Angela
1
你的 Web 服务 URL 中有一个拼写错误:wwww 而不是 www。 - Rudi Angela
显示剩余6条评论
2个回答

2
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>

0

你有那个工具的 Swift 3 实现吗? - Ray White
还没有@RayWhite,实际上我看到更多人对我的另一个工具json4swift.com感兴趣,所以我可能很快就会停止这个。 - Syed Absar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接