来自Windows Phone的Web服务调用

时间:2014-07-05 10:40:08

标签: c# web-services windows-phone-7 windows-phone-8 soap

大家好日子,

我想问你一个简单的问题。从Windows手机拨打肥皂网服务有哪些方法?

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

我想查看一些有关在Windows手机中调用和使用基于肥皂的Web服务的可能性的信息以及可能的示例。

谢谢。

方法"添加服务参考"总是在错误后返回我:

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

1 个答案:

答案 0 :(得分:1)

您可以使用HttpWebRequest来调用soap webservice。

下面的代码用于执行调用基于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);

服务之后调用你将在函数

中获得响应
void serviceConnection_OnEndResponse(string response, int statusCode)
        {
         MessageBox.Show(response);
         }

这是类服务连接

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