Swift Post Soap Service

时间:2019-04-10 10:23:25

标签: swift soap

我正在尝试发布两个参数以在下面的代码中使用,我仔细研究了如何在Internet上快速通过SOAP表单调用Web服务的答案,并找到了一些答案。我试图实现在这些答案中找到的代码,但是当我运行应用程序时,我没有收到任何错误,并且服务也没有得到任何结果。下面的代码将数据发布到Soap Service是正确的吗?

import UIKit

    class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, XMLParserDelegate {

        @IBOutlet var _username: UITextField!
        @IBOutlet var _password: UITextField!
        @IBOutlet var _login_button: UIButton!
        var mutableData:NSMutableData  = NSMutableData()
        var currentElementName:String = ""

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.

        }

        @IBAction func LoginBıutton(_ sender: AnyObject) {

            let username = _username.text
            let password = _password.text

            if(username == "" || password == ""){
                return
            }

            DoLogin(username!,password!)
        }

        func DoLogin(_ user:String, _ psw:String){

            do{

            let soapMessage = String(format: "<?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><Login xmlns='http://tempuri.org/'><TC>string</TC><PhoneNumber>string</PhoneNumber></Login></soap:Body></soap:Envelope>",user,psw)

            let urlString = "http://testing.rota.net.tr/Services/KentWorkTime.asmx"
            let url = URL(string: urlString)
            let theRequest = NSMutableURLRequest(url: url!)
            let msgLength = soapMessage.count
            theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")

            theRequest.addValue(String(msgLength), forHTTPHeaderField: "Content-Length")
            theRequest.httpMethod = "POST"
            theRequest.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)

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

            }catch let error{
               print(error.localizedDescription)
            }



        }

        func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
            mutableData = NSMutableData()
        }


        func connection(_ connection: NSURLConnection, didReceive data: Data) {
            self.mutableData.append(data)
        }

        func connectionDidFinishLoading(_ connection: NSURLConnection) {

            let xmlParser = XMLParser(data: mutableData as Data)
            xmlParser.delegate = self as! XMLParserDelegate
            xmlParser.parse()
            xmlParser.shouldResolveExternalEntities = true
        }

        func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
            currentElementName = elementName
        }

        func parser(_ parser: XMLParser, foundCharacters string: String) {
            if currentElementName == "LoginResult" {
                print(string)
            }
        }


    }

0 个答案:

没有答案