Calling a webservice within a HTA

A

Allister Farn

I have created a HTA from which I need to call a .NET webservice. I've
written the HTA to use the webservice.htc behaviour, but am wondering
if there is a better way. I am trying to limit the number of "advanced"
steps the users need to carry out (i.e. savnig files, downloading other
files to the same directory etc).

The scenario is as follows:

The user needs to download a report that requires user input, and they
need to save the report to their local file system, as they have to
provide input to the report when they are offline.

The user then goes online at a later stage, re-opens the report and
clicks on a Submit button which validates the report content, and then
calls a webservice passing a long string which is the data collected
via the report.

The web service then processes, validates and saves this data, and
returns either a success value or an error to the HTA, which is
displayed to the user.

Because it is not possible to reference webservice.htc using a http url
within the HTA, it appears that it is necessary for the user to
download and save the webservice.htc file to the same directory as
where they'll be saving the HTA. I'd prefer to avoid this step if
possible.

Can you please suggest other ways in which this functionality can be
provided?
 
M

Martin Honnen

Allister said:
I have created a HTA from which I need to call a .NET webservice. I've
written the HTA to use the webservice.htc behaviour, but am wondering
if there is a better way.
The user then goes online at a later stage, re-opens the report and
clicks on a Submit button which validates the report content, and then
calls a webservice passing a long string which is the data collected
via the report.

The web service then processes, validates and saves this data, and
returns either a success value or an error to the HTA, which is
displayed to the user.
Can you please suggest other ways in which this functionality can be
provided?

If you want to do it with script and all the web service gets is some
string and all the web service returns is some success value then you
could consider using MSXML, DOMDocument and XMLHTTP object to construct
the rather simple SOAP message, POST it to the service with the SOAP
action set as an HTTP request header, and receive and parse the rather
simple response. Of course the web service behavior or the SOAP toolkit
free you from dealing with SOAP yourself and in general it is easier and
more reliable to use such tools than dealing with SOAP itself but if you
don't want to install anything but have HTA support on the client then
MSXML as installed by IE is already available. The web service behavior
uses MSXML anyway.
 
F

farnsy

Can you please confirm that it's as simple as the following:

var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");

function sendData() {
xmlhttp.onreadystatechange = doHttpReadyStateChange;

var sendString = "test";

xmlhttp.Open("POST",
"http://servername/servicename/Service1.asmx/operationname", true);

xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Length", sendString.length);
xmlhttp.send("doc="+sendString);
}

function doHttpReadyStateChange(){
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responsetext);
var str = xmlhttp.responsetext;
}
}

I was messing around trying to create soap packets and having a lot of
trouble with things, and then this worked.

What should I be aware of with regards to the status property, and the
readystate property? What event/action will occur if the post times
out?

thanks for your help.
 
M

Martin Honnen

Can you please confirm that it's as simple as the following:

var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");

function sendData() {
xmlhttp.onreadystatechange = doHttpReadyStateChange;

var sendString = "test";

xmlhttp.Open("POST",
"http://servername/servicename/Service1.asmx/operationname", true);

xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");

You were talking about a web service used with the web service behavior
and that means to me the client so far made SOAP requests. If you now
switch to application/x-www-form-urlencoded requests then you need to
make sure that service expects/allows/understands such requests. It is
possible to configure .NET web services that way but not the default I
think.
xmlhttp.setRequestHeader("Content-Length", sendString.length);
xmlhttp.send("doc="+sendString);

If you want to send the request with the content type
application/x-www-form-urlencoded then your script needs to make sure
the argument to send is in that encoding, with JScript (as supported in
IE 5.5 and later) you could do
xmlhttp.send("doc=" + encodeURIComponent(sendString));
function doHttpReadyStateChange(){
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responsetext);
var str = xmlhttp.responsetext;

There is no standard defined I think what format a response to such a
HTTP POST request to a web service should have but I think .NET services
still answer with an XML document so consider using responseXML and
accessing it with DOM and not using responseText. Check the
documentation for that service what kind of answers it gives.

What should I be aware of with regards to the status property, and the
readystate property?

It is HTTP so the xmlhttp.status property gives you the HTTP response
status code the server sends, those are defined here:
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1>
 
M

Martin Honnen

xmlhttp.setRequestHeader("Content-Length", sendString.length);
xmlhttp.send("doc="+sendString);

Forgot to mention, if you want to set Content-Length then you need to
set it to the complete length of the string argument of the send method
so do e.g.
var body = "doc=" + encodeURIComponent(sendString);
xmlhttp.setRequestHeader("Content-Length", body);
xmlhttp.send(body);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top