小编Ser*_*PAL的帖子

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

我想为Swift 2调用Web服务.但它永远不会工作.这是我的代码.

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 …
Run Code Online (Sandbox Code Playgroud)

soap web-services swift swift2

2
推荐指数
1
解决办法
4844
查看次数

标签 统计

soap ×1

swift ×1

swift2 ×1

web-services ×1