调用SOAP Web服务的转换

时间:2018-03-07 17:23:35

标签: c# asp.net vb.net

首先,对这个问题道歉。让我先写一下,我已尝试使用以下代码调用SOAP使用C#完成的VB.NET网络服务。现在我很难将代码转换为public void CallService(string username, string password) { HttpWebRequest request = CreateSOAPWebRequest(); XmlDocument SOAPReqBody = new XmlDocument(); SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> <SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""> <SOAP-ENV:Header> <wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""> <wsse:UsernameToken> <wsse:Username>" + username + @"</wsse:Username> <wsse:Password>" + password + @"</wsse:Password> </wsse:UsernameToken> </wsse:Security> </SOAP-ENV:Header> <SOAP-ENV:Body> <OTA_PingRQ xmlns=""http://www.opentravel.org/OTA/2003/05"" EchoToken=""abc123"" TimeStamp=""2016-07-12T10:00:29.0Z"" Version=""1""> <EchoData> Hello World </EchoData> </OTA_PingRQ> </SOAP-ENV:Body> </SOAP-ENV:Envelope>"); using (Stream stream = request.GetRequestStream()) { SOAPReqBody.Save(stream); } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (WebResponse Serviceres = request.GetResponse()) { using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream())) { var ServiceResult = rd.ReadToEnd(); lblMsg.Text = ServiceResult; } } } public HttpWebRequest CreateSOAPWebRequest() { HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"https://cmtpi.siteminder.com/pmsxchangev2/services/CITRUS"); Req.Headers.Add(@"SOAP:Action"); Req.ContentType = "text/xml;charset=\"utf-8\""; Req.Accept = "text/xml"; Req.Method = "POST"; return Req; }

VB.NET

以上是一个正常工作的代码,我的问题是当我尝试转换它时using并且引用错误很少甚至导入关键字(而不是{{1} } C#}中显示的错误如下:我不是VB.NET的专家,只是希望有一些方向可以使其工作(Google搜索但无法找到合适的解决方案)

Public Sub CallService(ByVal username As String, ByVal password As String)
    Dim request As HttpWebRequest = CreateSOAPWebRequest()
    Dim SOAPReqBody As XmlDocument = New XmlDocument()

        SOAPReqBody.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>
                            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http:schemas.xmlsoap.org/soap/envelope/"">
                              <SOAP-ENV:Header>
                                <wsse:Security xmlns:wsse=""http:'docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
                                  <wsse:UsernameToken>
                                       <wsse:Username>" + username + "</wsse:Username>
                                       <wsse:Password>" + password + "</wsse:Password>
                                    </wsse:UsernameToken>
                                 </wsse:Security>
                             </SOAP-ENV:Header>
                             <SOAP-ENV:Body>
                               <OTA_PingRQ xmlns=""http:'www.opentravel.org/OTA/2003/05"" EchoToken=""abc123"" TimeStamp=""2016-07-12T10:00:29.0Z"" Version=""1"">
                                 <EchoData> Hello World </EchoData>
                               </OTA_PingRQ>
                             </SOAP-ENV:Body>
                             </SOAP-ENV:Envelope>")

        Imports (Stream stream = request.GetRequestStream())
        {
    SOAPReqBody.Save(Stream)
        }

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Imports (WebResponse Serviceres = request.GetResponse())
        {
            Imports (StreamReader rd = New StreamReader(Serviceres.GetResponseStream()))
            {
    Dim ServiceResult As Var = rd.ReadToEnd()
    Console.WriteLine(ServiceResult)

    Console.ReadLine()
            }
        }
    End Sub

Public Function CreateSOAPWebRequest() As HttpWebRequest
    Dim Req As HttpWebRequest = CType(WebRequest.Create("https://cmtpi.siteminder.com/pmsxchangev2/services/CITRUS"), HttpWebRequest)
    Req.Headers.Add("SOAP:Action")
        Req.ContentType = "text/xml;charset=\"utf-8\""
    Req.Accept = "text/xml"
    Req.Method = "POST"
    Return Req
End Function

Sample Code

3 个答案:

答案 0 :(得分:1)

VB代码的一个问题是,您需要使用下划线字符和连接运算符(&amp;)指示字符串文字何时长于一行。例如:

exampleliteral = _
"First few words of sentence that is longer than one line, " _
& "more words on second line, " _
& "end of sentence."

MSDN documentation about it here

Stack Overflow question and answers about it here

答案 1 :(得分:1)

您可以使用工具自动转换大部分代码,但有许多代码可用,但有几个例子是http://converter.telerik.com/https://www.developerfusion.com/tools/convert/csharp-to-vb/

你可能会发现它在多行分割字符串上有点窒息。但是,解决起来并不难。除了使用&而不是+进行连接之外,在VB.NET和C#中如何声明字符串没有太大区别。您可能需要使用_在多行文本之间加入。如果您有其他问题,可以在语言文档中轻松查找所有这些语法。

P.S。在您的手动尝试中,使用VB的Imports代替C#的using是不正确的。 VB中的直接等价只是Using。有关VB.NET中两个关键字的详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statementhttps://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/imports-statement-net-namespace-and-type

答案 2 :(得分:0)

在c#

中使用@时,使用字符串插值和$ in vb
Dim str = $"<?xml version=""1.0"" encoding=""utf-8""?>
                              <SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">
                              <SOAP-ENV:Header>
                                <wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
                                  <wsse:UsernameToken>
                                       <wsse:Username>" + username + $"</wsse:Username>
                                       <wsse:Password>" + password + $"</wsse:Password>
                                    </wsse:UsernameToken>
                                 </wsse:Security>
                             </SOAP-ENV:Header>
                             <SOAP-ENV:Body>
                               <OTA_PingRQ xmlns=""http://www.opentravel.org/OTA/2003/05"" EchoToken=""abc123"" TimeStamp=""2016-07-12T10:00:29.0Z"" Version=""1"">
                                 <EchoData> Hello World </EchoData>
                               </OTA_PingRQ>
                             </SOAP-ENV:Body>
                             </SOAP-ENV:Envelope>"