I was recently tasked with upgrading a classic ASP web application to C#. Everything has been fine, except that there is some code that uses Server.CreateObject("Msxml2.ServerXMLHTTP.3.0"). I really don’t even know what this is, except I have the general idea that it is being used to call a web service over HTTPS somehow.
The code looks like this:
Dim strSOAPEnvelope
strSOAPEnvelope = "<?xml version='1.0' encoding='utf-8'?>" & _
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
"<soap:Body>..........</soap:Body>" & _
"</soap:Envelope>"
oXMLHttp.Open "POST", "https://example.com/service.asmx", False
oXMLHttp.setRequestHeader "Content-Type", "text/xml"
oXMLHttp.setOption 2, 13056
oXMLHttp.setRequestHeader "Connection", "close"
oXMLHttp.setRequestHeader "SOAPAction", "https://example.com" & strfunction
I think that I understand the general notion that this is communicating with a web service via HTTPS. How do I go about upgrading this code to C#
This is make a SOAP request to a web service.
The equivalent feature for you is
HttpWebRequest, but Web Services in .NET is a full fledged feature. I wouldn’t bother creating XML and handling the response.If you are using .NET 3.0 or higher; you can use Windows Communication Foundation (WCF) to add a service reference. The link provides ample instructions on how to do that. Once you’ve added the service reference, you can call the service’s methods using plain C#. All you need to do is point it to the URL which contains the WSDL for the service.
If you are using .NET 2.0, you can use Web References which should work for SOAP as well.