2

I have a classic asp file that needs to connect to a web service. I am confused as to how to define the URL for the web service and the Namespace in the SOAPAction. When I run my code and write a Response.Write for the return value of the method that I am calling in the web service, it either returns the wsdl or the web page for the service

This code displays the web service html as if you are entering the web service .svc url:

Dim strSoapReq
strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
strSoapReq = strSoapReq & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
strSoapReq = strSoapReq & "<s:Body>"

strSoapReq = strSoapReq & "<TestMethod xmlns=""http:<serverName:<port>/PagingService/PagingService"">"
strSoapReq = strSoapReq & "</TestMethod>"
strSoapReq = strSoapReq & "</s:Body>"
strSoapReq = strSoapReq & "</s:Envelope>"

'Create server-side component to make requests  
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")    
URL = "http:<serverName:<port>/PagingService/PagingService.Paging.svc"

httpRequest.Open "GET", URL, False 
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
httpRequest.setRequestHeader "SOAPAction", URL & "/TestMethod"
httpRequest.Send(strSoapReq) 

Dim strResult
strResult = httpRequest.responseText
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)

If I add the ?wsdl to the end of the service url, it shows the WSDL. How can I call the method in the web service?

UPDATE I changed my code to the following:

Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "http://<server>/PagingService/"
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"

 Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")    
URL = "http://<server>/PagingService/PagingService.Paging.svc?WSDL"

httpRequest.Open "POST", URL, False 
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"    
httpRequest.setRequestHeader "SOAPAction", "http://<server>/PagingService/TestMethod"

' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param 

' Creates an XML DOM object.
Set DOM = CreateObject("MSXML2.DOMDocument.6.0") 

' Creates the main elements.
Set Envelope = DOM.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
DOM.appendChild Envelope
Set Body = DOM.createElement("soap:Body")
Envelope.appendChild Body 

' Creates an element for the TestMethod function.
Set Operation = DOM.createNode(1, "TestMethod", NS)
Body.appendChild Operation 

' Releases the objects.
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing

httpRequest.Send(DOM.xml) 

Dim strResult
strResult = httpRequest.status
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)

The http.Status result with this code is: 415 - unsupported media I saw a post with this error and they corrected it by changing the Content-Type. When I tried this: httpRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"

I got the status value of 400 - Bad request.

Gloria Santin
  • 2,066
  • 3
  • 51
  • 124
  • What happens if you leave out the charset=utf-8 in the header? – Sourcery Feb 26 '16 at 14:24
  • So it should read: `httpRequest.setRequestHeader "Content-Type", "text/xml"`? – Gloria Santin Feb 26 '16 at 14:45
  • So, I changed the line above and there was no change. Also, I tried using 'GET' and 'POST' in the `Open` statement. The 'GET' returns the wsdl or the website describing the web service. The 'POST' returns nothing. Neither has errors. – Gloria Santin Feb 26 '16 at 14:54
  • Is the namespace in the SOAP request suppose to be a URN? How do you determine the URN of your web service? – Gloria Santin Feb 26 '16 at 17:07
  • I checked the http status of my request and I am getting the error: 415 which is an 'unsupported media type'. – Gloria Santin Feb 26 '16 at 19:43
  • If you suspect you have the wrong content-type, check out my question here - http://stackoverflow.com/questions/35022415/serverxmlhttp-appending-to-content-type - Even if you take out the `charset=utf-8`, it sometimes still gets appended. – Andrew Tevington Mar 03 '16 at 02:50
  • I don't think the problem is my character type. Whether it is `text/xml` or `application/soap+xml`, the character type is `charset=uft-8`. I think my problem is that classic asp needs to be SOAP 1.1 and my web service is SOAP 1.2. I am trying another approach by using GET instead of POST. I can send a GET request and the WSDL is returned with a status of OK. If I can figure out how to call the method using a GET request, I am on my way. – Gloria Santin Mar 03 '16 at 14:23

1 Answers1

1

Finally figured this one out. Below is my code in case some poor minion needs to do this...maybe it will lessen the pain and shorten the time to get it to run. I connected a WCF web service (file extension, .svc) to a classic .asp file. I had to add a basicHttpBinding to the web service. It can also be secure (HTTPS). This is the binding added to the service config. file: (Note the name of it. defined by address attribute.

<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
</endpoint>

Added this to the bindings part of the config file:

<basicHttpBinding> 
    <binding name="BasicHttpBinding_IPaging"></binding>
</basicHttpBinding>

You need to recompile the project so the WSDL has this binding in its definition. Now the fun part....classic asp :( written in VBScript. The SOAP envelope gave me the most trouble:

'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace.  If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
'This should also be in your WSDL.  Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"

'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
 URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"

 'This was the hard part for me.  Defining the damn soap message
 'XML DOM objects.
 Dim Envelope, Body, Operation
 Dim ParamUserID, ParamUserName, 

 'Creates an XML DOM object.
 Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0") 
 objXmlDoc.async = false

 'Creates the main elements.
 Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
 Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
 Envelope.setAttribute "xmlns:xsi", NS_XSI
 Envelope.setAttribute "xmlns:xsd", NS_XSD
 objXmlDoc.appendChild Envelope

 Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
 Envelope.appendChild Body 

 'Creates an element for the SendPageForGalvanonSystem function.
 Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
 Body.appendChild Operation 

 'Add all the parameters to the DOM
 Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
 ParamUserID.text = strUserID
 Operation.appendChild ParamUserID

 Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
 ParamUserName.text = strUserName
 Operation.appendChild ParamUserName

 Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
 httpRequest.Open "POST", URL, False         
 httpRequest.setRequestHeader "Content-Type", "text/xml"     
 httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
 httpRequest.send objXmlDoc.xml

 'Releases the objects.
 Set ParamUserID = Nothing
 Set ParamUserName = Nothing
 Set Operation = Nothing
 Set Body = Nothing
 Set Envelope = Nothing      
Gloria Santin
  • 2,066
  • 3
  • 51
  • 124