How to update a VBScript application that uses Msxml2.ServerXMLHTTP.3.0 for C #?

I was recently tasked with upgrading a classic ASP web application to C #. Everything was fine, except that there is code that uses Server.CreateObject("Msxml2.ServerXMLHTTP.3.0") . I don’t even know what it is, except that I have a general idea that it is sometimes used to call a web service via HTTPS.

The code is as follows:

  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 opinion that this is communication with a web service via HTTPS. How do I upgrade to this code to C #

+4
source share
2 answers

This is a SOAP request for a web service.

The equivalent function for you is HttpWebRequest , but web services in .NET are a full-fledged function. I would not create XML and process the response.

If you are using .NET 3.0 or higher; You can use the Windows Communication Foundation (WCF) to add a link to the service . The link contains detailed instructions on how to do this. After you add a service reference, you can call the service methods using simple C #. All you have to do is specify the URL containing the WSDL for this service.

If you are using .NET 2.0, you can use Web References , which should also work for SOAP.

+3
source

If this is the only 1: 1 transformation, you can simply use the WebRequest instance to retrieve the data and evaluate it manually. This is what the old code does.

A more complicated, recommended, and simple way would be to get the webservice description (wsdl file), import it, and use the proxy classes that VS automatically created for you from wsdl.

VS: Project -> AddWebreference - enter the URL of the service (https://example.com/service.asmx)

If all goes well, it's just as easy to add a managed assembly link for your project. VS even keeps track of service updates.

Related review: http://msdn.microsoft.com/en-us/library/bb907578.aspx

And if you use VS: http://msdn.microsoft.com/en-us/library/tydxdyw9.aspx

+2
source

Source: https://habr.com/ru/post/1396294/


All Articles