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

问题描述

限时删除!!

我正在努力研究如何使用iOS中的SOAP从Web服务传递和检索数据。我有这个网址但该示例在请求时没有参数。我想拥有像

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
我使用下面的代码修复了问题:

// Swift3I 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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:37