SOAP Client in C# without access to a WSDL-file

前端 未结 4 1782
北荒
北荒 2021-02-02 17:17

I\'m working with a third party to integrate some of our systems with theirs and they provide us with a SOAP interface to make certain requests and changes in their connected sy

相关标签:
4条回答
  • 2021-02-02 17:49

    If you write a class that derives from System.Web.Services.Protocols.SoapHttpClientProtocol (and has the correct attributes, e.g., WebServiceBinding, SoapDocumentMethod, etc. applied to it and its methods), you can fairly easily call SOAP methods without needing the WSDL file.

    The easiest way to do this would probably be to write your own ASP.NET web service that replicates the third party's SOAP API, generate a proxy class from it, then manually edit the file to ensure that the URL, namespaces, method names, parameter types, etc. are correct for the third-party API you want to call.

    0 讨论(0)
  • 2021-02-02 17:55

    I haven't built a SOAP interface without access to a WSDL file, but the format is fairly well-documented. Your best bet might be to create a simplified WSDL file of your own that reflects what you know of the service you're subscribing to....

    If you decide to go this route, an existing stackoverflow question points at some tools for validating your WSDL.

    0 讨论(0)
  • 2021-02-02 18:01
    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);
            }
        }
    
    0 讨论(0)
  • 2021-02-02 18:15

    the code here is in VB.NET but I think you'll get the idea. The following is a client that invokes the 'processConfirmation' method and it expects a response (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
    

    And you use it by doing the following:

    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)
    

    I think this should work .... success!

    0 讨论(0)
提交回复
热议问题