使用WSDL使用SOAP WebService

时间:2015-08-26 12:25:26

标签: ios web-services soap wsdl

我已经完成了在SWIFT中使用SOAP WeService的教程:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

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

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        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>0</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"



        var urlString = "http://www.w3schools.com/webservices/tempconvert.asmx"

        var url = NSURL(string: urlString)
        var theRequest = NSMutableURLRequest(URL: url!)
        var msgLength = String(count(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(connection: 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: [NSObject : AnyObject]) {
        currentElementName = elementName
    }

    func parser(parser: NSXMLParser, foundCharacters string: String?) {
        if currentElementName == "CelsiusToFahrenheitResult" {
            println(string)
        }
    }

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


}

这是有效的,我得到温度转换,我想在我的网络服务上实现这个,但我不知道该怎么做,我有:

但是我不知道怎么做因为在教程中我把xml放在一个变量中,我把服务器的url放在哪里,以及如何调用wsdl文件的函数?

1 个答案:

答案 0 :(得分:1)

对于Swift 2.0,您可以在以下位置为您的WSDL生成Soap客户端: http://wsdl2swift.com 然后通过创建请求对象来利用操作:

enter image description here