Dinesh Gurrapu Dinesh Gurrapu - 3 months ago 6
Objective-C Question

How to pass parameters in SOAP web service having a Method?

I am getting confusion to use the soap service below one. Till now I never used SOAP based services. This is my service inthis service I need to pass 4 vlaues with parameters like username,password,ipaddress,devicename.And the output will come in json format. How to achieve this Help me.
http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx?op=EmployeesLoginMethod

Answer

All you need is just a HTTP post request and pass the right XML data and set the HTTP headers:

func soapRequest(username:String, password:String, ipAddress:String, deviceName:String){
        if let url = NSURL.init(string: "http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx"){
            let request = NSMutableURLRequest(URL: url)
            let requestBody = "<?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><EmployeesLoginMethod xmlns=\"http://tempuri.org/\"><username>\(username)</username><password>\(password)</password><IpAddress>\(ipAddress)</IpAddress><deviceName>\(deviceName)</deviceName></EmployeesLoginMethod></soap:Body></soap:Envelope>"
            request.HTTPMethod = "POST"
            request.HTTPBody = requestBody.dataUsingEncoding(NSUTF8StringEncoding)
            request.setValue("text/xml", forHTTPHeaderField: "Content-Type")
            request.setValue("\"http://tempuri.org/EmployeesLoginMethod\"", forHTTPHeaderField: "SOAPAction")
            NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
                guard error == nil && data != nil else{
                    //handle error
                    return
                }
                if let responseString = String.init(data: data!, encoding: NSUTF8StringEncoding){
                    print("\(responseString)")
                }
                //..........
                //Parse your XML response data
                //.........
            }).resume()
            
        }
        
    }

Comments