我们有一个新的会计系统,它为外部客户提供Web服务界面。我想访问其中一个接口(interface),但是没有WSDL,所以我通过使用HttpWebRequest创建了请求,并且工作正常。

但是,为了简化请求的创建和响应的解析,我想创建某种自动映射功能。我在XSD文件中有请求和响应结构。有任何想法吗?

请求创建:

public void SendRequest()
{
    HttpWebRequest request = CreateWebRequest();
    XmlDocument soapEnvelopeXml = new XmlDocument();
    soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
        <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
        <soap:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">

            ++ structure type inserted here ++

        </soap:Body>
        </soap:Envelope>");

    using (Stream stream = request.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }

    using (WebResponse response = request.GetResponse())
    {
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            Console.WriteLine(soapResult);
        }
    }
}

最佳答案

好吧,如果您真的没有办法保存适当的WSDL文件,但拥有XSD:s,则可以使用xsd.exe工具来创建映射到您的请求和响应类型的类。

这样的事情(在Visual Studio命令提示符中运行)

xsd.exe TheRequest.xsd /c /n:Your.Namespace
xsd.exe TheResponse.xsd /c /n:Your.Namespace

但是,实际上,请尽力找到WSDL,它将使您的生活更加轻松。

关于c# - .Net-使用不带WSDL的Web服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6096849/

10-14 10:30