Swift中的SOAP Web服务 [英] SOAP webservice in Swift

查看:87
本文介绍了Swift中的SOAP Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力研究如何使用iOS中的SOAP从Web服务传递和检索数据。我有这个网址 http://www.w3schools.com/webservices/tempconvert.asmx我将用它进行测试。我尝试使用SOAPEngine,但需要许可证。任何人都可以知道如何实现这一目的的免费库或示例代码?

I'm strugling working on how to pass and retrieve data from web service using SOAP in iOS. I have this url http://www.w3schools.com/webservices/tempconvert.asmx and I will used this for testing. I try SOAPEngine but it needs a licence. Can anyone know a free library or sample code on how to achieve this?

我在使用SOAP的IOS Swift调用Web服务但该示例在请求时没有参数。我想拥有像 http://www.w3schools.com/这样的价值观xml / tempconvert.asmx?op = CelsiusToFahrenheit

I found this tutorial in IOS Swift Call Web Service using SOAP but the example doesn't have parameter when requesting. I want to have a value like the http://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit

唯一缺少的是这部分:

let is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.w3schools.com/\"><soapenv:Header/><soapenv:Body><cgs:CelsiusToFahrenheit/><cgs:Celcius>20<cgs:Celcius/></soapenv:Body></soapenv:Envelope>"

这是我的完整代码:

import UIKit

class ViewController: UIViewController {

    let is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:CelsiusToFahrenheit/>cgs:Celcius>20<cgs:Celcius/></soapenv:Body></soapenv:Envelope>"


    override func viewDidLoad() {
        super.viewDidLoad()

        let is_URL: String = "http://www.w3schools.com/xml/CelsiusToFahrenheit"

        let lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
        let session = NSURLSession.sharedSession()
        //var err: NSError?

        lobj_Request.HTTPMethod = "POST"
        lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
        lobj_Request.addValue("www.w3schools.com", forHTTPHeaderField: "Host")
        lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        lobj_Request.addValue(String(is_SoapMessage.characters.count), forHTTPHeaderField: "Content-Length")

        lobj_Request.addValue("http://www.w3schools.com/xml/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")

        let task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Body: \(strData)")

            if error != nil
            {
                print("Error: " + error!.description)
            }

        })
        task.resume()

    }

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


}

如何更改当我需要提交20的价值时...

how to change this when I need to submit a value of 20...

提前致谢。

推荐答案

// Swift3
我使用下面的代码修复了问题:

// Swift3 I fixed the problem using the code below:

var is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.w3schools.com/webservices/\"><soapenv:Header/><soapenv:Body><cgs:CelsiusToFahrenheit><cgs:Celsius>20</cgs:Celsius></cgs:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>"


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

        let is_URL: String = "http://www.w3schools.com/webservices/tempconvert.asmx"

        let lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
        let session = NSURLSession.sharedSession()
        //let err: NSError?

        lobj_Request.HTTPMethod = "POST"
        lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
        lobj_Request.addValue("www.w3schools.com", forHTTPHeaderField: "Host")
        lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        lobj_Request.addValue(String(is_SoapMessage.characters.count), forHTTPHeaderField: "Content-Length")
        lobj_Request.addValue("http://www.w3schools.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")

        let task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Body: \(strData)")

            if error != nil
            {
                print("Error: " + error!.description)
            }

        })
        task.resume()

    }

这篇关于Swift中的SOAP Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆