ios - 使用 WSDL 使用 SOAP WebService

标签 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 文件的函数?

最佳答案

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

enter image description here

关于ios - 使用 WSDL 使用 SOAP WebService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226641/

相关文章:

c# - 在 C# 客户端中捕获 SOAP 消息

ios - 无法在 UIWebview 中显示宏启用 word 文件

java - 避免 RPC (SOAP) API 之间的中间数据转换?

ios - Storyboard中的自定义单元格行高设置没有响应

json - 如何在swift 3中转换xml和json数据

java - wsimport 上的 WSDL 错误

web-services - 如何过滤服务器上的 Backbone 集合

python - 将列表与元组与列表进行比较并返回唯一的元组

ios - 在弹出 segue 为 'dismissed' 后刷新父 View 上的 IBOutlet 外观

ios - 将弹出窗口添加到以编程方式添加的 UIBarButtonItem 并将图像添加到相同的弹出窗口 Swift