views:

517

answers:

2
var oXMLDoc, oXMLHttp, soapRequest, soapResponse;

oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");

oXMLHttp.open("POST", "http://nerdbox/HelloService.svc", false);

// Add HTTP headers
oXMLHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oXMLHttp.setRequestHeader("SOAPAction", "http://tempuri.org/IHelloService/SayHello");

// Form the message
soapRequest = '<?xml version="1.0" encoding="utf-16"?><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"&gt;&lt;soap:Body&gt;&lt;SayHello xmlns="http://tempuri.org/"&gt;&lt;name&gt;Zuhaib&lt;/name&gt;&lt;/SayHello&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;';

WScript.Echo("Request : " + soapRequest);

oXMLHttp.send(soapRequest);
soapResponse = oXMLHttp.responseXML.xml;
WScript.Echo("Respose : " + soapResponse);

Whats wrong with this JScript? why am I getting 400 Bad Request. I read similar threads in stackoverflow .. some say its soap message formatting problem.

This is what the message looks like if I take it from fiddler.

+1  A: 

Try changing your action from IHelloService to HelloService.

And let me ask you, why are you doing it the hard way. Just add a webHttpBinding and use JSON.

See a very easy example here.

Sky Sanders
The actual service not written by me. I am just trying to connect. The script has to be consumed from vbscript or jscript. Json is jscript only.
Zuhaib
ok, dont use json.. try sending as query or form first. it is possible those protocols are enabled. munging soap packets is painful and might be unnecessary.... see the early sections in the linked article, it covers that scenario..
Sky Sanders
never mind, i am having a brain fart. you are polling a WCF service. sorry...
Sky Sanders
lol .. Another reason for using SOAP is the webservice can change depending on customer .. it could be written in Java etc and we can't demand them to write JSON webservices.The Json thing works very well though ..
Zuhaib
A: 

I had to change your code to the following to get it to run in VBSEdit...then I (obviously) got the error about it not being able to find the resource...but change your code to this and see if it makes a difference?

Dim oXMLDoc, oXMLHttp, soapRequest, soapResponse

Set oXMLHttp = CreateObject("Microsoft.XMLHTTP")

oXMLHttp.open "POST", "http://nerdbox/HelloService.svc", False

'// Add HTTP headers
oXMLHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
oXMLHttp.setRequestHeader "SOAPAction", "http://tempuri.org/IHelloService/SayHello"

'// Form the message
soapRequest = "<?xml version=""1.0"" encoding=""utf-16""?><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""&gt;&lt;soap:Body&gt;&lt;SayHello xmlns=""http://tempuri.org/""&gt;&lt;name&gt;Zuhaib&lt;/name&gt;&lt;/SayHello&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;"

WScript.Echo "Request : " + soapRequest

oXMLHttp.send soapRequest
soapResponse = oXMLHttp.responseXML.xml
WScript.Echo "Respose : " + soapResponse
Shawn de Wet
@Shawn: please reformat your code so that XML isn't all on one line.
John Saunders
@JohnSaunders: hey I just copied the code from the original post and reposted with the changes I had to make to get it to compile and run in VBSEdit...the XML-in-one-line came from the original post, and I was not concerned with changing that to get the code to work.
Shawn de Wet
@Shawn: my concern now isn't whether the program works, but whether other SO users can read it.
John Saunders
Nevermind.. I had to write a JSON wrapper for the service and consume using JScript.
Zuhaib