【问题标题】:SOAP Client in C# without access to a WSDL-fileC# 中的 SOAP 客户端,无需访问 WSDL 文件
【发布时间】:2008-11-10 14:51:10
【问题描述】:

我正在与第三方合作,将我们的一些系统与他们的系统集成,他们为我们提供了一个 SOAP 接口,以便在他们连接的系统中提出某些请求和更改。对我来说,问题是他们没有提供 WSDL 文件供我使用。如果我有一个 WSDL 文件,只需运行提供的 .NET 命令 (wsdl.exe) 并生成一个代理类来与服务交互就很简单了。

在没有 WSDL 文件的情况下,是否有一种“简单”的方法可以做到这一点?我拥有我们可以访问的所有功能以及我需要发送的参数以及我应该期望的回报。

拥有没有 WSDL 文件的 SOAP 服务是否常见? (我问这个是因为我们将来会添加更多的外部系统)

有没有人针对无 WDSL 服务做过代理类或任何其他形式的客户端,并且对如何做有任何好的指导?

【问题讨论】:

  • 我现在在同一条船上......你有没有找到任何方法来完成这件事?
  • 遗憾的是没有。事实证明,我试图访问的 SOAP 在许多其他方面也是无效的,因此该项目被废弃了。

标签: c# .net soap wsdl


【解决方案1】:
string EndPoints = "http://203.189.91.127:7777/services/spm/spm";

string New_Xml_Request_String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><OTA_AirLowFareSearchRQ EchoToken=\"0\" SequenceNmbr=\"0\" TransactionIdentifier=\"0\" xmlns=\"http://www.opentravel.org/OTA/2003/05\"><POS xmlns=\"http://www.opentravel.org/OTA/2003/05\"><Source AgentSine=\"\" PseudoCityCode=\"NPCK\"  TerminalID=\"1\"><RequestorID ID=\"\"/></Source><YatraRequests><YatraRequest DoNotHitCache=\"true\" DoNotCache=\"false\" MidOfficeAgentID=\"\" AffiliateID=\"\" YatraRequestTypeCode=\"SMPA\"/></YatraRequests></POS><TravelerInfoSummary><AirTravelerAvail><PassengerTypeQuantity Code=\"ADT\" Quantity=\"1\"/><PassengerTypeQuantity Code=\"CHD\" Quantity=\"1\"/><PassengerTypeQuantity Code=\"INF\" Quantity=\"1\"/></AirTravelerAvail></TravelerInfoSummary> <SpecificFlightInfo><Airline Code=\"\"/></SpecificFlightInfo><OriginDestinationInformation><DepartureDateTime>" + DateTime.Now.ToString("o").Remove(19, 14) + "</DepartureDateTime><OriginLocation CodeContext=\"IATA\" LocationCode=\"DEL\">" + Source + "</OriginLocation><DestinationLocation CodeContext=\"IATA\" LocationCode=\"BOM\">" + Destincation + "</DestinationLocation></OriginDestinationInformation><TravelPreferences><CabinPref Cabin=\"Economy\"/></TravelPreferences></OTA_AirLowFareSearchRQ></soapenv:Body></soapenv:Envelope>";


 protected string HttpSOAPRequest_Test(string xmlfile, string proxy)
    {
        try
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.InnerXml = xmlfile.ToString();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(EndPoints);
            req.Timeout = 100000000;
            if (proxy != null)
                req.Proxy = new WebProxy(proxy, true);
            req.Headers.Add("SOAPAction", "");
            req.ContentType = "application/soap+xml;charset=\"utf-8\"";
            req.Accept = "application/x-www-form-urlencoded"; //"application/soap+xml";
            req.Method = "POST";
            Stream stm = req.GetRequestStream();
            doc.Save(stm);
            stm.Close();
            WebResponse resp = req.GetResponse();
            stm = resp.GetResponseStream();
            StreamReader r = new StreamReader(stm);
            string myd = r.ReadToEnd();
            return myd;
        }

   catch (Exception se)
        {
            throw new Exception("Error Occurred in AuditAdapter.getXMLDocumentFromXMLTemplate()", se);
        }
    }

【讨论】:

    【解决方案2】:

    如果您编写一个派生自 System.Web.Services.Protocols.SoapHttpClientProtocol 的类(并且具有正确的属性,例如,WebServiceBindingSoapDocumentMethod 等应用于它及其方法),您可以相当轻松地调用 SOAP 方法而无需WSDL 文件。

    最简单的方法可能是编写自己的 ASP.NET Web 服务来复制第三方的 SOAP API,从中生成代理类,然后手动编辑文件以确保 URL、命名空间、方法名称、参数类型等是否适合您要调用的第三方 API。

    【讨论】:

      【解决方案3】:

      我还没有构建一个不访问 WSDL 文件的 SOAP 接口,但格式是fairly well-documented。您最好的选择可能是创建一个您自己的简化 WSDL 文件,以反映您对所订阅服务的了解......

      如果您决定走这条路,existing stackoverflow question 指向一些用于验证您的 WSDL 的工具。

      【讨论】:

        【解决方案4】:

        这里的代码在 VB.NET 中,但我想你会明白的。以下是调用“processConfirmation”方法的客户端,它需要响应 (MyBase.SendRequestResponse)。

        Imports Microsoft.Web.Services3
        Imports Microsoft.Web.Services3.Addressing
        Imports Microsoft.Web.Services3.Messaging
        
        Namespace Logic
            Public Class HTTPClient
                Inherits Soapclient
        
                Sub New(ByVal destination As EndpointReference)
                    MyBase.Destination = destination
                End Sub
        
                <SoapMethod("processConfirmation")> _
                Public Function processConfirmation(ByVal envelope As SoapEnvelope) As SoapEnvelope
                    Return MyBase.SendRequestResponse("processConfirmation", envelope)
                End Function
            End Class
        End Namespace
        

        您可以通过以下方式使用它:

        Dim hc As New HTTPClient(New Microsoft.Web.Services3.Addressing.EndpointReference(New System.Uri("http://whatever.srv")))
        
        Dim envelope As New Microsoft.Web.Services3.SoapEnvelope
        Dim doc As New Xml.XmlDocument
        doc.LoadXml("<hey>there</hey>")
        envelope.SetBodyObject(doc)
        
        Dim return_envelope As Microsoft.Web.Services3.SoapEnvelope = hc.processConfirmation(envelope)
        

        我认为这应该可行....成功!

        【讨论】:

        • -1:这需要 WSE 3.0,它已过时,除非没有其他选择,否则不应使用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        相关资源
        最近更新 更多