Simple ASP scripting question?

C

Cliff

I'm not that great with ASP but I've hacked together a simple server
side script that uses the Microsoft XML ServerXMLHTTP object. The
problem is that when other people use the script sometimes they get an
error that the object can not be created, due likely to the required
Microsoft XML libraries not being installed. I'd like to try creating
the XMLHTTP object in an if/else block so that if the object can not be
created then it tries another object creation approach... here is the
code I'm using to create the object:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

Can someone provide me with the code snippet that will result in the
variable objSrvHTTP having a ServerXMLHTTP object inside that works?
Also, if there are other variations on that object name then please
include those objects as well...

FYI, I'm only using open, setRequestHeader, send and responseText
methods on the object.

Thanks,
Cliff.
 
M

Martin Honnen

Cliff wrote:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

var progIds = [
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];
var httpRequest = null;
for (var i = 0, l = progIds.length; i < l; i++) {
try {
httpRequest = new ActiveXObject(progIds);
break;
}
catch (e) {}
}
if (httpRequest != null) {
// make your request here
}
else {
// no object found
}

There are also MSXML 5 and 6 so depending on what you do expect on the
server you could use e.g.

var progIds = [
'Msxml2.ServerXMLHTTP.6.0',
'Msxml2.ServerXMLHTTP.5.0',
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];
 
M

McKirahan

Cliff said:
I'm not that great with ASP but I've hacked together a simple server
side script that uses the Microsoft XML ServerXMLHTTP object. The
problem is that when other people use the script sometimes they get an
error that the object can not be created, due likely to the required
Microsoft XML libraries not being installed. I'd like to try creating
the XMLHTTP object in an if/else block so that if the object can not be
created then it tries another object creation approach... here is the
code I'm using to create the object:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

Can someone provide me with the code snippet that will result in the
variable objSrvHTTP having a ServerXMLHTTP object inside that works?
Also, if there are other variations on that object name then please
include those objects as well...

FYI, I'm only using open, setRequestHeader, send and responseText
methods on the object.

Will this help? You can test is as-is.

<html>
<head>
<title>xmlhttpx.html</title>
<script type="text/javascript">
function getRequestObj() {
var ret = null;
var xml = [
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"];
if (window.ActiveXObject) {
for (var i=0; i<xml.length; i++) {
try {
ret = new ActiveXObject(xml);
break;
} catch(e) {}
}
} else if(window.XMLHttpRequest) {
try {
ret = new XMLHttpRequest();
} catch(e) {}
}
return ret;
}

var sURL = "http://www.gabocorp.com/";
var oXML = getRequestObj();
oXML.open("GET",sURL,false);
oXML.send();

alert(oXML.responseText);
</script>
</head>
<body>
</body>
</html>


Also, check out this link:

Identify which components are installed on the server.
<URL:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8976&lng
WId=4>

It can be adapted to run on any user's PC as a VBS file instead of ASP.
Just remove "<%", "%>", "Server.", and "<BR>"
and change "Response.Write" to "WScript.Echo"
then save it as "Components.vbs" and run it via the command line
"cscript.exe //nologo Components.vbs > Components.txt"
 
C

Cliff

This looks like it worked perfectly. Is there any advantage or
disadvantage to the order of object creation, for example, are newer
versions of the object more security constrained? Also, is it possible
to avoid going through the loop each time the page is called once the
appropriate library has been determined?

Cliff.


Martin said:
Cliff wrote:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

var progIds = [
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];
var httpRequest = null;
for (var i = 0, l = progIds.length; i < l; i++) {
try {
httpRequest = new ActiveXObject(progIds);
break;
}
catch (e) {}
}
if (httpRequest != null) {
// make your request here
}
else {
// no object found
}

There are also MSXML 5 and 6 so depending on what you do expect on the
server you could use e.g.

var progIds = [
'Msxml2.ServerXMLHTTP.6.0',
'Msxml2.ServerXMLHTTP.5.0',
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];
 
M

Martin Honnen

Cliff said:
Is there any advantage or
disadvantage to the order of object creation, for example, are newer
versions of the object more security constrained?

There are differences, consult the MSXML SKD for details, it is online here
Also, is it possible
to avoid going through the loop each time the page is called once the
appropriate library has been determined?

Change the code to store the program id that could be successfully
created and used that program id from there one.
 
A

Anthony Jones

Cliff said:
I'm not that great with ASP but I've hacked together a simple server
side script that uses the Microsoft XML ServerXMLHTTP object. The
problem is that when other people use the script sometimes they get an
error that the object can not be created, due likely to the required
Microsoft XML libraries not being installed. I'd like to try creating
the XMLHTTP object in an if/else block so that if the object can not be
created then it tries another object creation approach... here is the
code I'm using to create the object:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

Can someone provide me with the code snippet that will result in the
variable objSrvHTTP having a ServerXMLHTTP object inside that works?
Also, if there are other variations on that object name then please
include those objects as well...

FYI, I'm only using open, setRequestHeader, send and responseText
methods on the object.

I just stick with MSXML2.ServerXMLHTTP.3.0 it's always there on Win2K above
and I know exactly what I'm getting and what it can and can't do.
 
L

Larry Bud

Cliff.


Martin said:
Cliff wrote:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP.4.0");

The above line often fails because the object doesn't exist so I would
like to trap the error and try to create the object like:

var objSrvHTTP=Server.CreateObject("MSXML2.ServerXMLHTTP");

var progIds = [
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];
var httpRequest = null;
for (var i = 0, l = progIds.length; i < l; i++) {
try {
httpRequest = new ActiveXObject(progIds);
break;
}
catch (e) {}
}
if (httpRequest != null) {
// make your request here
}
else {
// no object found
}

There are also MSXML 5 and 6 so depending on what you do expect on the
server you could use e.g.

var progIds = [
'Msxml2.ServerXMLHTTP.6.0',
'Msxml2.ServerXMLHTTP.5.0',
'Msxml2.ServerXMLHTTP.4.0',
'Msxml2.ServerXMLHTTP.3.0',
'Msxml2.ServerXMLHTTP'
];

This looks like it worked perfectly. Is there any advantage or
disadvantage to the order of object creation, for example, are newer
versions of the object more security constrained? Also, is it possible
to avoid going through the loop each time the page is called once the
appropriate library has been determined?

Why even do that? I mean, the object is on the server, not the client.
Just figure out which one you have on your server and store it in an
application variable.

If you maintain your own server, it's even easier, since you should
know what libraries are on it and if/when they're updated.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top