简体   繁体   English

调用SOAP Web服务的转换

[英]Conversion of Calling SOAP Web Service

First of all, apologies for this question. 首先,对这个问题表示歉意。 Let me write first, I've tried the below code for calling a SOAP web service using C# that worked perfectly. 让我先写,我尝试了下面的代码,使用C#完美地调用了SOAP Web服务。 Now I am stuck with the conversion of the code into VB.NET : 现在,我一直坚持将代码转换为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;
 }

The above is a working code and my problem is when I try to convert it VB.NET and there are few errors with quotations and even the Import keyword (Instead of using in C# ) shows error as follows: I am not that expert with VB.NET and would just expect some directions to make it work (Googled but unable to find the appropriate solution) 上面是一个有效的代码,我的问题是当我尝试将其转换为VB.NET ,引号中几乎没有错误,甚至Import关键字(而不是在C#using )也显示如下错误:我不是VB.NET专家VB.NET ,并且只希望获得一些指导即可使其工作(Googled,但无法找到合适的解决方案)

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

样例代码

One of the problems with your VB code is that you need to indicate when a string literal is longer than one line using the underscore character and concatenation operator (&). VB代码的问题之一是,您需要使用下划线字符和串联运算符(&)来指示字符串文字何时长于一行。 Example: 例:

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 有关此的MSDN文档

Stack Overflow question and answers about it here 堆栈溢出问题和答案在这里

You can convert the majority of the code automatically using a tool, there are many available, but a couple of examples are http://converter.telerik.com/ or https://www.developerfusion.com/tools/convert/csharp-to-vb/ . 您可以使用工具自动转换大多数代码,有很多可用的工具,但是http://converter.telerik.com/https://www.developerfusion.com/tools/convert/csharp有几个示例-to-vb /

You may find it chokes a bit on the multi-line split string. 您可能会发现它在多行拆分字符串上有点窒息。 However, it's not too hard to resolve. 但是,解决起来并不难。 Other than the use of & instead of + for concatenation, there's not much difference in how Strings are declared in VB.NET and C#. 除了使用&而不是+进行串联之外,在VB.NET和C#中声明字符串的方式没有太大区别。 You might need to join between the multiple lines of text using _ . 您可能需要使用_在多行文本之间连接。 You can easily look up all this syntax in the language documentation if you have further issues with it. 如果您还有其他问题,可以在语言文档中轻松查找所有这些语法。

PS In your manual attempt, the use of VB's Imports in place of C#'s using is incorrect. PS在您的手动尝试中,使用VB的Imports代替C#的using是不正确的。 The direct equivalent in VB is simply Using . VB中的直接等效项只是Using See https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement and https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/imports-statement-net-namespace-and-type for details of the two keywords in VB.NET. 请参阅https://docs.microsoft.com/zh-CN/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有关VB.NET中两个关键字的详细信息。

Use string interpolation with $ in vb as you use @ in c# 在c#中使用@时,在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>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM