【问题标题】:Conversion of Calling SOAP Web Service调用 SOAP Web 服务的转换
【发布时间】:2018-03-07 17:23:35
【问题描述】:

首先,为这个问题道歉。让我先写,我已经尝试使用以下代码调用SOAP Web 服务,使用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 时,引号甚至 Import 关键字(而不是@987654334 中的using @) 显示错误如下:我不是 VB.NET 的专家,只是希望一些指导可以使它工作(谷歌搜索但无法找到合适的解决方案)

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

【问题讨论】:

标签: c# asp.net vb.net


【解决方案1】:

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

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

附:在您的手动尝试中,使用 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】:

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

    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

    【讨论】:

      【解决方案3】:

      在 vb 中使用 $ 字符串插值,就像在 c# 中使用 @ 一样

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        • 2023-03-14
        • 2013-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多