blackh4t_
blackh4t_

Reputation: 1

Web Service call from Windows Phone

Good day to you all,

I wanted to ask you a simple question. What are the ways to call a soap web service from windows phone?

I have tried using the Service reference (I add service reference to a ?wsdl URL and it generates all the methods I have on web service), however I came across the error (unmarshalling error, unexpected elements) in sending the request. Just to note I have created a soap web service in Java and all of the methods are functioning and returning data, both in iOS application and in Android application, however I am struggling with this in windows phone.

I wanted to check some information of possibilities of calling and consuming a soap based web service in windows phone and examples if possible.

Thank you.

The method "Add Service Reference" always returns me following error:

Unmarshalling Error: unexpected element (uri:"http://webservicelocation.com/",local:"param1"). Expected elements are <{}param1>,<{}param2>,<{}param3>

Upvotes: 0

Views: 718

Answers (1)

Praveen
Praveen

Reputation: 267

You can use HttpWebRequest for calling a soap webservice.

Below code is used to do a currency converter which calls a soap based webservice.

Inside MainPage

ServiceConnection cs = new ServiceConnection();

Inside Constructor

string pXml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://www.webserviceX.NET/"">" +
                                    "<soapenv:Header/><soapenv:Body><web:ConversionRate>" +
                                    "<web:FromCurrency>" + "USD" + "</web:FromCurrency>" +
                                    "<web:ToCurrency>" + "INR"+ "</web:ToCurrency>" +
                                    "</web:ConversionRate></soapenv:Body></soapenv:Envelope>";
                        ServiceConnection cs = new ServiceConnection();

                        cs.OnEndResponse += new ServiceConnection.OnServerEndResponse(serviceConnection_OnEndResponse);
                        cs.ServiceCall(pXml);

After the service Call u will get response inside the function

void serviceConnection_OnEndResponse(string response, int statusCode)
        {
         MessageBox.Show(response);
         }

Here is the class Service Connection

class ServiceConnection
    {
        public string url = "";
        private string postXml;
        public delegate void OnServerEndResponse(string response, int statusCode);
        public event OnServerEndResponse OnEndResponse;

        public ServiceConnection()
        {
            url = "http://www.webservicex.net/CurrencyConvertor.asmx";
        }

        public void ServiceCall(string pxml)
        {
            postXml = pxml;
            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "text/xml;charset=UTF-8";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }

        void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
            string postData = postXml;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }

        void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string Response = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    OnEndResponse(Response, Convert.ToInt32(response.StatusCode));
                });

            }
            catch
            {
                OnEndResponse("", 500);
            }
        }

Upvotes: 1

Related Questions