简体   繁体   中英

System.InvalidOperationException: Missing parameter error when trying to consume web service from vbscript

I have the code below to consume a web service using javascript. The code works well when I invoke a Dummy method that has no parameters and just returns a string, but when I attempt to invoke a method that has a single parameter, XmlNode in this case, I get this error:

System.InvalidOperationException: Missing parameter: XmlNode.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

VBScript function:

Function ConsumeWebService(SamplePlan)
    Dim oXML_Aux

    Dim strURL
    Dim logger:Set logger = New EMR_Logging
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
    Dim strEnvelope

    logger.LogInfo "Invoking ConsumeWebService - StartTime: " &  Now
    Set oXML_Aux = CreateObject(DOMDocument)
    strEnvelope = "<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:tem=""http://tempuri.org/"">" _
       & " <soap:Header/>" _
       & " <soap:Body>" _
       & "   <tem:SyncSamplePlan_DCA>" _
       & "       <tem:XmlNode><![CDATA[" & SamplePlan & "]]></tem:XmlNode>" _
       & "    </tem:SyncSamplePlan_DCA>" _
       & " </soap:Body>" _
       & " </soap:Envelope>"

    Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
    oXMLHTTP.onreadystatechange = GetRef("HandleStateChange")

    strURL = "http://ocn-da2.lsecmes.rvoaustin.local/GNE_OCN_Webparts/QC_Sampling_Plan_Import/QC_Sample_Plan_Import.asmx/SyncSamplePlan_DCA"
    call oXMLHTTP.open("POST", strURL, False)

    call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded") 'orig
    oXML_Aux.loadXml strEnvelope

    call oXMLHTTP.send(oXML_Aux.xml)
    logger.LogInfo "Invoking Web Service- strSoapEnvelope: " & strEnvelope
End Function

Sub HandleStateChange
    if(oXMLHTTP.readyState = 4) then
        dim szResponse: szResponse = oXMLHTTP.responseText

       call oXMLDoc.loadXML(szResponse)

       if (oXMLDoc.parseError.errorCode <> 0) then
           call msgbox(oXMLDoc.parseError.reason)
       else
           call msgbox(oXMLDoc.getElementsByTagName("string")(0).childNodes(0).text)
       end if
   end If
End Sub

So basically my question is what I'm doing wrong? If you check the line where I populate the strEnvelope variable, I'm explicitly populating the XmlNode parameter. I took the Soap envelope for this web service method using Soap UI, and if go to the logs (you can see that I'm logging the strEnvelope variable value at the end of the first function), and get the value from this strEnvelope variable and put it in Soap UI, it works with no issues, so with this I can confirm that the Soap envelope I'm using for this method is ok. I suspect the issue might be related with the SetRequestHeader property I'm using, but I'm not sure.

Can anyone help me here?

Note: I'm executing the vbscript within the same server that hosts the web service. This is the response I get logged in Fiddler:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 17 Sep 2014 21:48:23 GMT
Connection: close
Content-Length: 404

System.InvalidOperationException: Missing parameter: XmlNode.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

I was able to solve it. The web service was always working if I invoked it from IE (when you open IIS, right click on the web service .asmx file, and click on browse), so I looked at the call in Fiddler and found out that the SOAP envelope that worked only had: XmlNode=<samples><sample>

instead of having the entire SOAP envelope I was using in my original post. So I found that when you see the service definition (when you open IIS, right click on the web service .asmx file, and click on browse), there are three examples, soap1.1 , soap1.2 and HTTP Post , so in this scenario I had to use the HTTP Post example. So this is how the code looks now:

So this is how I populate the Envelope now:

Dim strEnvelope:strEnvelope = "XmlNode=" & SamplePlan 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM