Reputation: 1157
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
Upvotes: 0
Views: 797
Reputation: 61784
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.
Upvotes: 1
Reputation: 2210
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
Upvotes: 1
Reputation: 2028
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>"
Upvotes: -1