我已经完成了在 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.
}
}
它有效,我得到了温度转换,我想在我的网络服务上实现这个,但我不知道该怎么做,我有:
- 网址:https://ip/webservice/contactfile.wsdl ,它包含我的 xml
- 这个网址:https://ip/webservice/server.php
但我不知道该怎么做,因为在教程中我将 xml 放入变量中,将服务器 url 放入何处,以及如何调用 wsdl 文件的函数?
最佳答案
对于 Swift 2.0,您可以在线为您的 WSDL 生成一个 Soap 客户端: http://wsdl2swift.com 然后通过创建请求对象来利用操作:
关于ios - 使用 WSDL 使用 SOAP WebService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226641/