Consuming a Soap Web Service with Swift

IOS Swift Call Web Service using SOAP

So i was being silly. The main thing was that I missed setting the body of the message to the SOAP request. My updated corrected code is below:

//
// ViewController.swift
// TestWebServiceSoap
//
// Created by George M. Ceaser Jr on 6/2/15.
// Copyright (c) 2015 George M. Ceaser Jr. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
var is_SoapMessage: String = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"><soapenv:Header/><soapenv:Body><cgs:GetSystemStatus/></soapenv:Body></soapenv:Envelope>"

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

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

@IBAction func btnClicked(sender: AnyObject)
{
var is_URL: String = "http://www.cgsapi.com/CGSWebService.asmx"

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

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

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

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

})
task.resume()
}

}

Consuming a SOAP web service with Swift

curl -s --header "content-type: text/xml" -d @request.xml http://www.w3school <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/"><CelsiusToFahrenheitResult>104</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>

Swift and web service with SOAP

I found a very good library to solve this issue. It's call SWXMLHash.
https://github.com/drmohundro/SWXMLHash
I only did this:

var lobj_Request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
var session = NSURLSession.sharedSession()
var err: NSError?
var user = nameTextField.text
var pass = passTextField.text
lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue(hostString, forHTTPHeaderField: "Host")
lobj_Request.addValue("application/soap+xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(soapMessage.characters.count), forHTTPHeaderField: "Content-Length")
lobj_Request.addValue(SOAP_ACTION, forHTTPHeaderField: "SOAPAction")
var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")
var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)")

var xml = SWXMLHash.parse(data!)

var propertyIwant = xml["soap:Envelope"]["soap:Body"]["SoapResponse"]["SomeField"]["propertyIwant"].element?.text

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

})
task.resume()

How to call the SOAP Request/Response in another Class in Swift Code?

Okay, finally works.
In this example, I use a button click event for Web Service call.

CallWebService.swift (without XML parsing) :

class CallWebService: UIViewController
{

func BuildMessage1(Rupp: String)-> String
{
var is_SoapMessage = "<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><HelloAppleAndroid xmlns='http://testestest.org/'><RUPP>\(Rupp)</RUPP></HelloAppleAndroid></soap:Body></soap:Envelope>"

return is_SoapMessage
}

func HelloWebService(Rupp: String)
{
var is_SoapMessage = BuildMessage1(Rupp)

var is_URL: String = "http://testesttest/webservice.asmx"

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

lobj_Request.HTTPMethod = "POST"
lobj_Request.HTTPBody = is_SoapMessage.dataUsingEncoding(NSUTF8StringEncoding)
lobj_Request.addValue("testest.it", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://testest.org/HelloAppleAndroid", forHTTPHeaderField: "SOAPAction")

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

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

task.resume()
}

}

In ViewController.swift:

@IBAction func btnClicked(sender: AnyObject)
{
var getRupp = "ARGTEST093467555"
CallWebService().HelloWebService(getRupp)
}


Related Topics



Leave a reply



Submit