Link to home
Start Free TrialLog in
Avatar of durick
durickFlag for United States of America

asked on

use XMLHTTP to read a web service

here is my code that does nothing ??
Sub plireader()

    Dim Request As XMLHTTP
    Dim Parameter1 As String
    Dim Doc As DOMDocument60
   
    Parameter1 = "demo"
   
    Set Request = New XMLHTTP
    Set Doc = New DOMDocument60
    '"http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"
    With Request
        .Open "POST", "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence", False
        .setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
        .send "company=" & Parameter1 'Name & Value of the Parameter
        Doc.loadXML .responseText
    End With
    Doc.loadXML Doc.Text
MsgBox Doc.XML
End Sub
Avatar of zc2
zc2
Flag of United States of America image

I've changed your code a little:
    With Request
        .Open "POST", "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence", False
        .setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
        .send "company=" & Parameter1 'Name & Value of the Parameter
        Doc.loadXML .responseText
    End With
MsgBox Doc.XML

Open in new window

Now it prints the server response which contains an error:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was
unable to process request. ---&gt; Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail/></soap:Fault></soap:Body></soap:Envelope>

Open in new window

I think, the server expects some SOAP-like XML to be sent, not just URL-encoded parameters.
Avatar of Flabio Gates
Flabio Gates

zc2 is correct that the server expects a SOAP request.
Clicking on the URL also shows (near the very top)
The test form is only available for requests from the local machine.
which means that the URL-encoded request only works when issued from local machine, i.e., from where the server is running.
Avatar of durick

ASKER

ok here is my new code still not working
Sub plireader()
Dim strrequest As String
Dim objhttp As Object
    strXmlToSend = "<company>demo</company>"
webserviceurl = "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"

 Set objhttp = CreateObject("MSXML2.XMLHTTP.3.0")
MsgBox "send now"
  strrequest = "<?xml version=""1.0"" encoding=""utf-8""?>" _
               & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope"">" _
               & "<soap:Header>" _
               & "<soap:Body>" _
               & "<soap:Request>" _
               & "<soap:Company>demo</soap:Company>" _
               & "</soap:Request>" _
               & "</soap:Header>" _
               & "</soap:Body>" _
               & "</soap:Envelope>"
objhttp.Open "GET", webserviceurl, False
objhttp.setRequestHeader "Content-Type", "text/xml"
objhttp.send strrequest
  MsgBox objhttp.responseText
    Set objhttp = Nothing
    Set XMLDOC = Nothing
End Sub
Your xml request still does not match what the web service expects. Try sending
<?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>
    <CEMgetAttendence xmlns="http://www.pli.edu/public/CEM/">
      <company>string</company>
    </CEMgetAttendence>
  </soap:Body>
</soap:Envelope>

Open in new window

Avatar of durick

ASKER

ok herte is my new code as you suggested: still not working

Sub plireader()
Dim strrequest As String
Dim objhttp As Object
    strXmlToSend = "<company>demo</company>"
webserviceurl = "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"

 Set objhttp = CreateObject("MSXML2.XMLHTTP.3.0")
MsgBox "send now"
  strrequest = "<?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>" _
               & "<CEMgetAttendence xmlns=""https://www.pli.edu/public/CEM/"">" _
               & "<company>demo</company>" _
               & "</CEMgetAttendence>" _
               & "</soap:Body>" _
               & "</soap:Envelope>"

MsgBox strrequest
objhttp.Open "GET", webserviceurl, False
objhttp.setRequestHeader "Content-Type", "text/xml"
objhttp.send strrequest
    Set objhttp = Nothing
    Set XMLDOC = Nothing
End Sub
According to the service description page ( https://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence ) you need to pass the SOAPAction http header. Add to your script the following line:
objhttp.setRequestHeader "SOAPAction", "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"

Open in new window

Change the value of webserviceurl from  "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence" to "https://www.pli.edu/public/CEM/Service.asmx"

Change the line which starts the content of the SOAP envelope body to
& "<CEMgetAttendence xmlns=""http://www.pli.edu/public/CEM/"">" _

Open in new window

( The namespace should be "http://www.pli.edu/public/CEM/", not "https://www.pli.edu/public/CEM/" )
Avatar of durick

ASKER

added your changes not sure if working
Sub plireader()
Dim strrequest As String
Dim objhttp As Object
    strXmlToSend = "<company>demo</company>"
webserviceurl = "https://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendance"

 Set objhttp = CreateObject("MSXML2.XMLHTTP.3.0")
MsgBox "send now"
  strrequest = "<?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>" _
               & "<CEMgetAttendence xmlns=""http://www.pli.edu/public/CEM/"">" _
               & "<company>demo</company>" _
               & "</CEMgetAttendence>" _
               & "</soap:Body>" _
               & "</soap:Envelope>"

MsgBox strrequest
objhttp.Open "GET", webserviceurl, False
objhttp.setRequestHeader "SOAPAction", "http://www.pli.edu/public/CEM/Service.asmx"
objhttp.setRequestHeader "Content-Type", "text/xml"
objhttp.send strrequest
MsgBox "response" & objhttp.responseText
    Set objhttp = Nothing
    Set XMLDOC = Nothing
End Sub
Why did you change the HTTP method to "GET"? It should be "POST" since you are sending the content.
In the previous comment I suggested to add the following header:
objhttp.setRequestHeader "SOAPAction", "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"
but you reduced it to
objhttp.setRequestHeader "SOAPAction", "http://www.pli.edu/public/CEM/Service.asmx"
Because of that causes the server responds with a failure:
<soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: http://www.pli.edu/public/CEM/Service.asmx.</faultstring><detail /></soap:Fault>

Open in new window


instead of the correct response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CEMgetAttendenceResponse xmlns="http://www.pli.edu/public/CEM/"><CEMgetAttendenceResult><CEM xmlns="" /></CEMgetAttendenceResult></CEMgetAttendenceResponse></soap:Body></soap:Envelope>

Open in new window

Avatar of durick

ASKER

getting closer
Sub plireader()
Dim strrequest As String
Dim objhttp As Object
    strXmlToSend = "<company>demo</company>"
webserviceurl = "https://www.pli.edu/public/CEM/Service.asmx"

 Set objhttp = CreateObject("MSXML2.XMLHTTP.3.0")
MsgBox "send now"
  strrequest = "<?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>" _
               & "<CEMgetAttendence xmlns=""http://www.pli.edu/public/CEM/"">" _
               & "<company>demo</company>" _
               & "</CEMgetAttendence>" _
               & "</soap:Body>" _
               & "</soap:Envelope>"

MsgBox strrequest
objhttp.Open "POST", webserviceurl, False
objhttp.setRequestHeader "SOAPAction", "http://www.pli.edu/public/CEM/Service.asmx?op=CEMgetAttendence"
objhttp.setRequestHeader "Content-Type", "text/xml"
objhttp.send strrequest
MsgBox "response" & objhttp.responseText
    Set objhttp = Nothing
    Set XMLDOC = Nothing
End Sub
Your code looks fine, but the server www.pli.edu is seemingly down.
Avatar of durick

ASKER

thanks will try later today
Avatar of durick

ASKER

srill getting error  = the parameter is incorrect at this statement
 objhttp.send strrequest
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of durick

ASKER

that worked - thanks so much for all your help and handholding
You are welcome
The question author was apparently satisfied by this answer.