C# 中的 SOAP 客户端无法访问 WSDL 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/278037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:14:58  来源:igfitidea点击:

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

c#.netsoapwsdl

提问by Kristoffer L

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 systems. The problem for me is that they do not supply a WSDL-file for me to work against. If I had a WSDL-file it would be a simple matter just to run the supplied .NET command (wsdl.exe) and generate a proxy class to interact with the service.

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

Is there an "easy" way to do this without a WSDL-file? I have all the functions that we can access and what parameters I need to send and what I should expect in return.

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

Is it common to have a SOAP-service without WSDL-files? (I'm asking this since we're going to add more external systems into the mix in the future)

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

Has anyone done a proxy-class or any other form of client against a WDSL-less service and have any good pointers on how to do it?

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

回答by cori

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....

我还没有在无法访问 WSDL 文件的情况下构建 SOAP 接口,但该格式的文档相当齐全。您最好的办法可能是创建一个您自己的简化 WSDL 文件,以反映您对所订阅服务的了解......

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

如果您决定走这条路线,现有的 stackoverflow 问题会指向一些用于验证 WSDL 的工具。

回答by Bradley Grainger

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.

如果你写一个类,派生自System.Web.Services.Protocols.SoapHttpClientProtocol(并且具有正确的属性,如WebServiceBindingSoapDocumentMethod等适用于它和它的方法),你可以很容易调用SOAP方法无需在WSDL文件。

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.

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

回答by SpoBo

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).

这里的代码在 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

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!

我认为这应该有效......成功!

回答by Manikant Thakur

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