在Swift 2中如何调用SOAP web服务?

在Swift 2中如何调用SOAP web服务?,swift,web-services,soap,swift2,Swift,Web Services,Soap,Swift2,我想打电话给Swift 2的网络服务。但它从来都不起作用。这是我的密码 import UIKit class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate { var mutableData:NSMutableData = NSMutableData.init() var currentElementName:NSStri

我想打电话给Swift 2的网络服务。但它从来都不起作用。这是我的密码

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

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



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


    @IBAction func actionConvert(sender : AnyObject) {
        let celcius = txtCelsius.text

        let 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/xml/'><Celsius>\(celcius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"


        let urlString = "http://www.w3schools.com/xml/tempconvert.asmx"

        let url = NSURL(string: urlString)

        let theRequest = NSMutableURLRequest(URL: url!)


        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue((soapMessage), forHTTPHeaderField: "Content-Length")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false

        let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
        connection!.start()


    }




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

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



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

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


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

    }


    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if currentElementName == "CelsiusToFahrenheit" {
            txtFahrenheit.text = string
        }
    }
导入UIKit
类ViewController:UIViewController、UITextFieldDelegate、NSURLConnectionDelegate、NSXMLParserDelegate{
var-mutableData:NSMutableData=NSMutableData.init()
var currentElementName:NSString=“”
@IBOutlet var txtCelsius:UITextField!
@IBOutlet var txtFahrenheit:UITextField!
@iAction func actionConvert(发件人:AnyObject){
让celcius=txtCelsius.text
让soapMessage=“\(celcius)”
让URL字符串=”http://www.w3schools.com/xml/tempconvert.asmx"
让url=NSURL(字符串:urlString)
让theRequest=NSMutableURLRequest(URL:URL!)
addValue(“text/xml;charset=utf-8”,用于httpheaderfield:“内容类型”)
theRequest.addValue((soapMessage),用于HttpHeaderField:“内容长度”)
theRequest.HTTPMethod=“POST”
theRequest.HTTPBody=soapMessage.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:false)//或false
let connection=NSURLConnection(请求:theRequest,委托:self,开始立即:true)
连接!.start()
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func连接(连接:NSURLConnection!,DIDReceiverResponse响应:NSURResponse!){
可变数据长度=0;
}
func连接(连接:NSURLConnection!,didReceiveData:NSData!){
mutableData.appendData(数据)
}
func ConnectionIDFinishLoading(连接:NSURLConnection!){
设xmlParser=NSXMLParser(数据:mutableData)
xmlParser.delegate=self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities=true
}
func解析器(解析器:NSXMLParser,foundCharacters string:string){
如果currentElementName==“CelsiUstofHarenheit”{
txtFahrenheit.text=字符串
}
}

NSURLConnection
已被弃用,请改用
NSURLSession

下面是一个函数的示例,该函数使用NSURLSession和回调执行您想要的操作:

func getFarenheit(celsius celsius: Int, completion: (result: String) -> Void) {
    let 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/xml/'><Celsius>\(celsius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"
    let urlString = "http://www.w3schools.com/xml/tempconvert.asmx"
    if let url = NSURL(string: urlString) {
        let theRequest = NSMutableURLRequest(URL: url)
        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue((soapMessage), forHTTPHeaderField: "Content-Length")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        NSURLSession.sharedSession().dataTaskWithRequest(theRequest) { (data, response, error) in
            if error == nil {
                if let data = data, result = String(data: data, encoding: NSUTF8StringEncoding) {
                    completion(result: result)
                }
            } else {
                print(error!.debugDescription)
            }
        }.resume()
    }
}
它打印包含XML和转换值的数据:

107.6


请格式化您的代码并解释错误。谢谢您的回答。您的代码是正确的,但我只想看107.6。我怎么做?不客气。我的回答告诉您如何修复连接部分,这是您的问题。现在您必须解析XML,为此,您可以像以前一样继续使用NSXMLParser-您只需要se来自NSURLSession的数据,而不是来自NSURLConnection委托的数据。:)
getFarenheit(celsius: 42) { (result) in
    print(result)
}