WSDL是否兼容Windows Phone 8?

4
我需要在Windows Phone 8应用程序中使用Web服务(WSDL),但在VS2012中无法正常工作。
例如:

http://chennaiemergency.co.in/sree/s2.php?wsdl

  1. 右键单击项目 > 添加服务引用
  2. 在地址文本框中粘贴URL
  3. 点击“Go”按钮
  4. 服务将显示所有操作
  5. 点击“ok”

服务已添加,但引用.cs文件中没有我的操作信息...

是否有其他使用wsdl的方法?

1个回答

4

我发现解决这个问题的最佳方法是手动发送SOAP请求。SOAP和wsdl与WP不太兼容。如果您有选择,也许可以选择WCF作为您的Web服务。

以下是在Windows窗体应用程序中进行SOAP请求的代码(在WP项目中,您必须使用异步方法-> beginGetRequestStream()和beginGetResponse()。关于此问题,MSDN上有很多文档):

        // Building of my XML 
        XNamespace env = "http://schemas.xmlsoap.org/soap/envelope/";
        XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
        XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
        XNamespace enc = "http://schemas.xmlsoap.org/soap/encoding/";
        XNamespace typens = "urn:...";
        XNamespace xsiType = "xsd:string";
        XElement soapEnv = new XElement(env + "Envelope",
            new XAttribute(XNamespace.Xmlns + "SOAP-ENV",env.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
            new XElement(env + "Body",
                new XAttribute(env + "encodingStyle",enc.NamespaceName),
                    new XElement(typens + "MethodName",
                        new XAttribute(XNamespace.Xmlns + "typens",typens.NamespaceName),
                        new XElement("elementName",
                            new XAttribute(xsi + "type",xsiType.NamespaceName), "...value"),
                        new XElement("elementName",
                            new XAttribute(xsi + "type",xsiType.NamespaceName),"...value"),
                        new XElement("elementName",
                            new XAttribute(xsi + "type",xsiType.NamespaceName),"...value")
        )));

        // HTTPWEBREQUEST
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("...url...");
        webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        webRequest.Method = "POST";
        webRequest.KeepAlive = false;
        webRequest.ContentType = "text/xml; charset=utf-8";
        webRequest.CookieContainer = new CookieContainer();

        webRequest.Headers.Add("SOAPAction", "...webservice link...");
        webRequest.ProtocolVersion = new Version(1,1);
        webRequest.Timeout = 1000;


        using (StreamWriter stream = new StreamWriter(webRequest.GetRequestStream()))
        { 
            stream.Write(soapEnv); 
            stream.Flush();
            stream.Close();            
        }


        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream()))
            {
                if (responseReader != null)
                {
                    .....code....
                    webResponse.Close();
                }
            }            
        }

1
-1 我希望我能够投两次反对票。永远不要通过字符串操作构建 XML。此外,您的 WebResponse 和 StreamReader 上缺少 using 块。 - John Saunders
谢谢您的评论,约翰!我要改进代码! - monstergold
编辑:添加使用块和使用LINQ构建XML。谢谢John Saunders! - monstergold
哇!仅用几个小时就从负数变成正数了。 - John Saunders

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接