0

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. Now I am stuck with the conversion of the code into 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)

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

user8512043
  • 1,043
  • 12
  • 20
  • 2
    Did you try with a tool such as http://converter.telerik.com/ or https://www.developerfusion.com/tools/convert/csharp-to-vb/? – ADyson Mar 07 '18 at 17:25
  • I've tried with the other one but failed. Will the above work? – user8512043 Mar 07 '18 at 17:27
  • Why not try it, it only takes a few seconds? Btw where did you get the idea that the equivalent of C#'s `using` was `Import` or `Imports` in VB.NET? AFAIK the equivalent is `Using`. https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement, https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/imports-statement-net-namespace-and-type – ADyson Mar 07 '18 at 17:28
  • http://converter.telerik.com/ both of your functions converted. – lcryder Mar 07 '18 at 17:30
  • Tried with the above tools and worked perfectly. But it's unable to convert the quotations that I've shown in the image - Section SOAPReqBody.LoadXml(). Any idea to resolve them @ADyson? – user8512043 Mar 07 '18 at 17:45
  • 1
    Other than the use of & instead of + for concatenation, there's not much difference in how Strings are declared in vb and c#. You might need to join between the multiple lines of code using &_. You can easily look up all this syntax in the language documentation – ADyson Mar 07 '18 at 18:13
  • Worked perfectly @ADyson and thanks a lot. You can post your comments as an answer, so that I can mark it. – user8512043 Mar 08 '18 at 09:00

3 Answers3

1

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

Stack Overflow question and answers about it here

stackuser83
  • 2,012
  • 1
  • 24
  • 41
1

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

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

P.S. In your manual attempt, the use of VB's Imports in place of C#'s using is incorrect. The direct equivalent in VB is simply Using. See https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement and https://learn.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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
-1

Use string interpolation with $ in vb as you use @ in 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>"
boruchsiper
  • 2,016
  • 9
  • 29
  • 53