从Windows Phone调用Web服务

本文关键字:Web 服务 调用 Phone Windows | 更新日期: 2023-09-27 18:11:37

大家好,

我想问你一个简单的问题。从windows phone调用soap web服务的方法有哪些?

我尝试使用服务引用(我将服务引用添加到wsdl URL,它生成我在web服务上的所有方法),但是我在发送请求时遇到了错误(解组错误,意外元素)。要注意的是,我用Java创建了一个soap web服务,所有的方法都在运行并返回数据,无论是在iOS应用程序还是在Android应用程序中,但是我在windows phone中遇到了这个问题。

我想检查一些关于在windows phone中调用和使用基于soap的web服务的可能性的信息,如果可能的话,还有一些例子。

谢谢。

方法"添加服务引用"总是返回以下错误:

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

从Windows Phone调用Web服务

你可以使用HttpWebRequest来调用soap web服务。

下面的代码是用来做一个货币转换器,它调用一个基于soap的web服务。

主页内

ServiceConnection cs = new ServiceConnection();

内部构造函数

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);

服务后调用u将得到函数内部的响应

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

下面是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);
            }
        }