How to access SOAP services from iPhone

后端 未结 7 1306
无人共我
无人共我 2020-11-22 09:34

I\'m planning to develop an app for the iPhone and that app would have to access a couple of SOAP services. While doing some basic checking in the iPhone SDK I was not able

7条回答
  •  迷失自我
    2020-11-22 10:21

    Here is a swift 4 sample code which execute API calling using SOAP service format.

       func callSOAPWSToGetData() {
    
            let strSOAPMessage =
                "" +
                    "" +
                    "" +
                    "" +
                    "50" +
                    "" +
                    "" +
            ""
    
            guard let url = URL.init(string: "http://www.example.org") else {
                return
            }
            var request = URLRequest.init(url: url)
            let length = (strSOAPMessage as NSString).length
            request.addValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
            request.addValue("http://www.yourapi.com/webservices/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
            request.addValue(String(length), forHTTPHeaderField: "Content-Length")
            request.httpMethod = "POST"
            request.httpBody = strSOAPMessage.data(using: .utf8)
    
            let config = URLSessionConfiguration.default
            let session = URLSession(configuration: config)
            let task = session.dataTask(with: request) { (data, response, error) in
                guard let responseData = data else {
                    print("Error: did not receive data")
                    return
                }
                guard error == nil else {
                    print("error calling GET on /todos/1")
                    print(error ?? "")
                    return
                }
                print(responseData)
                let strData = String.init(data: responseData, encoding: .utf8)
                print(strData ?? "")
            }
            task.resume()
        }
    

提交回复
热议问题