如何在 C# 中使用 Web 上不存在的 wsdl 文件 [英] How to consume a wsdl file that doesn't exist on web in C#
问题描述
我正在尝试向我的 Visual Studio 项目实施带有 SOAP 消息的服务.他们给了我一些说明,我在 stackoverflow 上看到我必须在 Visual Studio 中使用 wsdl 和 xsd 文件,以便它可以创建我需要的所有类.问题是,当我尝试将 wsdl 文件添加为服务引用时,它说该文件不存在.我使用提供的 URL 自己搜索它,但它确实不存在.有什么办法可以让我使用它.
I am trying to impliment a service with SOAP messages to my visual studio project. They gave me some instructions and I saw on stackoverflow that I have to consume the wsdl and xsd file in visual studio so that it can create all the classes that I need. The problem is that when I try to add the wsdl file as service reference it says that the file doesnt exist. I search it myself using the URL provided and it really doesnt exists. Is there any way so that I can use it.
Below is the WSDL statement:
https://www1.gsis.gr/wsicispay/rmt_ws?wsdl
<definitions targetNamespace="http://remittance_ws/rmt_ws.wsdl" name="rmt_ws"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://remittance_ws/rmt_ws.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema>
<xsd:import namespace="http://remittance_ws/rmt_ws.wsdl"
schemaLocation="https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1"/>
</xsd:schema>
</types>
<message name="remittanceXml">
<part name="parameters" element="tns:remittanceXml"/>
</message>
<message name="remittanceXmlResponse">
<part name="parameters" element="tns:remittanceXmlResponse"/>
</message>
<message name="UnsupportedEncodingException">
<part name="fault" element="tns:UnsupportedEncodingException"/>
</message>
<portType name="rmt_ws">
<operation name="remittanceXml">
<input message="tns:remittanceXml"/>
<output message="tns:remittanceXmlResponse"/>
<fault message="tns:UnsupportedEncodingException" name="UnsupportedEncodingException"/>
</operation>
</portType>
<binding name="rmt_wsBinding" type="tns:rmt_ws">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="remittanceXml">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="UnsupportedEncodingException">
<soap:fault name="UnsupportedEncodingException" use="literal"/>
</fault>
</operation>
</binding>
<service name="rmt_ws">
<port name="rmt_ws" binding="tns:rmt_wsBinding">
<soap:address location="https://www1.gsis.gr/wsicispay/rmt_ws"/>
</port>
</service>
</definitions>
这里是 xsd
https://www1.gsis.gr/wsicispay/rmt_ws?xsd=1
<xs:schema version="1.0" targetNamespace="http://remittance_ws/rmt_ws.wsdl"
xmlns:tns="http://remittance_ws/rmt_ws.wsdl" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="UnsupportedEncodingException" type="tns:UnsupportedEncodingException"/>
<xs:element name="remittanceXml" type="tns:remittanceXml"/>
<xs:element name="remittanceXmlResponse" type="tns:remittanceXmlResponse"/>
<xs:complexType name="remittanceXml">
<xs:sequence>
<xs:element name="inputXML" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="remittanceXmlResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="UnsupportedEncodingException">
<xs:sequence>
<xs:element name="message" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Δείγμα μηνύματος:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:rmt="http://remittance_ws/rmt_ws.wsdl">
<soapenv:Header/>
<soapenv:Body>
<rmt:remittanceXml>
<inputXML><remittance><wsTin>Tin</wsTin><wsUser>Username</wsUser><wsPswd
>Pswd</wsPswd><contain><bmrn>16GRIMXXXX00001746</bmrn><blrn>TESTFOR1&
lt;/blrn><bcnt>1</bcnt><bafm>099099099</bafm></contain><contain><bmr
n>16GRIMXXXX00001720</bmrn><blrn>TESTFOR2</blrn><bcnt>2</bcnt><bafm&
gt;099099099</bafm></contain><contain><bmrn>16GRIMXXXX00001738</bmrn><bl
rn>TESTFOR2</blrn><bcnt>2</bcnt><bafm>099099099</bafm></contain></
remittance></inputXML>
</rmt:remittanceXml>
</soapenv:Body>
</soapenv:Envelope>
那些是他们给我的 pdf 文件.
Those are in a pdf file that they gave me.
如果我将它添加为来自服务引用的文件,它会创建引用 ePayments 而没有 .cs 文件.当我添加引用时,它只给我一个服务 rmt_ws,它有一个操作 remitanceXML 而没有别的.我不知道这是否正确以及我应该如何使用它
If I add it as a file from service references it creates the reference ePayments and no .cs file. When I add the reference it gives me only one service rmt_ws which has an operation remitanceXML and nothing else. I don't know if this is correct and how should I use it
推荐答案
我记得几年前我在 Web 服务的身份验证方面遇到了问题,但我不记得我在哪里找到了这个替代解决方案:
I remember, that I had a problem with authentication of a web service some years ago, but I didn't remember where I found this alternative solution:
>
- 在visual studio中创建一个新的控制台应用程序(我不确定,如果不手动编辑项目文件等,删除现有项目中的旧服务引用是否有效)
- 像之前一样选择添加服务引用,但不要输入 wsdl 的路径.而是在对话框中按高级..."按钮,然后在下一个对话框中按添加 Web 引用..."按钮.
- 在新对话框中输入 wsdl 文件的路径,然后按添加引用"
在项目中打开文件program.cs",并在最后一个using..."行和namespace...."之间添加这个带有命名空间的类
- Create a new console app in visual studio (I am not sure, if deleting the old service reference in the existing project works without manual editing project file etc.)
- Select add service reference as before, but don't enter the path of a wsdl. Instead press the button "Advanced..." in the dialog and "Add Web reference..." in the next dialog.
- In the new dialog enter the path of the wsdl file and press "Add reference"
Open the file "program.cs" in the project and add this class with namespace between the last "using..." line and "namespace...."
namespace TestWeb.WebReference { partial class rmt_ws : System.Web.Services.Protocols.SoapHttpClientProtocol { protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)base.GetWebRequest(uri); if (PreAuthenticate) { System.Net.NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic"); if (networkCredentials != null) { System.Diagnostics.Debug.WriteLine("User: " + networkCredentials.UserName); System.Diagnostics.Debug.WriteLine("PW: " + networkCredentials.Password); byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer); } else { throw new ApplicationException("No network credentials"); } } return request; } }
用您项目的命名空间(通常是项目名称)替换TestWeb"(插入代码的第一行).
Replace "TestWeb" (first line of inserted code) with the namespace of your project (normally the project name).
用以下代码替换现有的类Program":
Replace the existing class "Program" with the following code:
class Program { static void Main(string[] args) { // Fill with a valid xml request String inputXML = ""; String answer = ""; try { WebReference.rmt_ws _webService = new WebReference.rmt_ws(); System.Net.CredentialCache myCredentials = new System.Net.CredentialCache(); // Set correct user and password System.Net.NetworkCredential netCred = new System.Net.NetworkCredential("User", "Password"); _webService.Credentials = netCred.GetCredential(new Uri(_webService.Url), "Basic"); _webService.PreAuthenticate = true; answer = _webService.remittanceXml(inputXML); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(answer); Console.ReadLine(); } }
为您的请求使用有效的 xml 设置 inputXML
Set inputXML with a valid xml for your request
如果这没有帮助,您应该真正打开一个新问题,寻找可以提供帮助的其他人.
If this does not help you should really open a new question to find some one other who could help.
这篇关于如何在 C# 中使用 Web 上不存在的 wsdl 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!