1

I have never used SOAP based services and I am unsure of using the follwing SOAP Service.

http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx?op=EmployeesLoginMethod

In this service I need to pass 4 values with the parameters: username, password, ipaddress and devicename. And the output is in JSON format.

Kindly help to achieve the desired result.

1

3 Answers 3

1

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()
            
        }
        
    }

Sign up to request clarification or add additional context in comments.

5 Comments

But In android they r getting reponse in Json Format where I am getting in Xml format.
Base on the API link you provide, the response will be in XML format. Although there might be a JSON api available somewhere.
where ineed to change that api
can u please help me out.
@DineshGurrapu The person supply you the API may have a Json version. Or you can do a search on converting XML to json, there is library can help u do that.
1

May this will help you

Here is the way to call SOAP in Objective-C :

NSURL *baseURL = [NSURL URLWithString:@"http://yourbaseURLxxxxxxxx"];

    NSString *sSOAPMessage = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                              "<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/\">\n"
                              "<soap:Body>\n"
                              "<EmployeesLoginMethod xmlns=\"http://tempuri.org/\">"
                              "<username>[email protected]</username>"
                              "<password>lovetocode</password>"
                              "<ipaddress>192.16.0.0</ipaddress>"
                              "<devicename>iPhone7</devicename>"
                              "</EmployeesLoginMethod>"
                              "</soap:Body>\n"
                              "</soap:Envelope>\n"];

    NSLog(@"-----Developed Envelope----%@",sSOAPMessage);



    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:@"http://tempuri.org/IService/EmployeesLoginMethod" forHTTPHeaderField:@"SOAPAction"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Data string = %@",string);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
    }];

    [operation start];

Comments

0

The output will be soap too from what I can see in the service definition.

I am assuming you want to call the function from another server using c# as of time of writing there is no indication on the used technology. You need to add wcf reference and call the method using the generated class.

To add a reference to a service in the current solution In Solution Explorer, right-click the name of the project that you want to add the service to, and then click Add Service Reference. The Add Service Reference dialog box appears. Click Discover. All services (both WCF Data Services and WCF services) in the current solution are added to the Services list. In the Services list, expand the node for the service that you want to use and select an entity set. In the Namespace box, enter the namespace that you want to use for the reference. Click OK to add the reference to the project. A service client (proxy) is generated, and metadata that describes the service is added to the app.config file.

MSDN How to: Add, Update, or Remove a WCF Data Service Reference


If you are posting from javascript, here is a vanilla js example.

var soap_post_body = ""+
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+ 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
'  <soap12:Body>'+
'    <EmployeesLoginMethod xmlns="http://tempuri.org/">'+
'      <username>string</username>'+
'      <password>string</password>'+
'      <IpAddress>string</IpAddress>'+
'      <deviceName>string</deviceName>'+
'    </EmployeesLoginMethod>'+
'  </soap12:Body>'+
'</soap12:Envelope>';

xhr = new XMLHttpRequest();
xhr.open('POST', 'http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx');
xhr.setRequestHeader('Content-Type', 'application/soap+xml; charset=utf-8');
xhr.onload = function() {
    if (xhr.status === 200) {
        console.log(xhr.responseText);
    }
    else {
        console.log(xhr.status);
    }
};
xhr.send(encodeURI(soap_post_body));

2 Comments

<NSHTTPURLResponse: 0x176bb930> { URL: workforce.wifisocial.in/WebServicesMethods/… } { status code: 500, headers { "Cache-Control" = private; "Content-Length" = 509; "Content-Type" = "application/soap+xml; charset=utf-8"; Date = "Fri, 02 Sep 2016 05:17:34 GMT"; Server = "Microsoft-IIS/8.0"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; "X-Powered-By-Plesk" = PleskWin; } }
response getting like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.