6

Does anyone know how to set up set up a default action for when a ServerXMLHTTP request times out? I'm using setTimeouts() to set the time out options according to the MSDN site.

Ideally I would like to initialize the request again from the beginning or refresh the page should it time out.

I'm using classic asp and jscript.

Here's my request:

function serverXmlHttp(url) {
    var serverXmlHttp;
    serverXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0");

    // set time out options
    serverXmlHttp.setTimeouts(15000,15000,15000,15000);

    // does not work
    // serverXmlHttp.ontimeout(Response.Write("page has timed out"));

    serverXmlHttp.open("GET", url, false);
    serverXmlHttp.send();

    if (serverXmlHttp.readyState == 4) {
        return serverXmlHttp.responseText;
    }
}
Choy
  • 2,087
  • 10
  • 37
  • 48

2 Answers2

5

The important thing is to find out why it is timing out ..

Is the remote Url on the same application as the calling page ?

if so have a look at INFO: Do Not Send ServerXMLHTTP or WinHTTP Requests to the Same Server as you will be facing thread starvation ..

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 1
    No, it is not. The request is being made to a database on another server. I just want to have a fail safe in case the request does time out (as it has done on occasion) rather than a generic time out error. – Choy Oct 13 '10 at 21:22
4

Figured it out. I just need to use a try/catch statement.

function serverXmlHttp(url) {

    try {
        var serverXmlHttp;
        serverXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0");

        // set time out options
        serverXmlHttp.setTimeouts(15000,15000,15000,15000);

        serverXmlHttp.open("GET", url, false);
        serverXmlHttp.send();

        if (serverXmlHttp.readyState == 4) {
            return serverXmlHttp.responseText;
        }
    catch(error) {
        // whatever I want to happen if there's an error
        Response.Write("Sorry, your request has timed out");
    }

}
Choy
  • 2,087
  • 10
  • 37
  • 48
  • 2
    If you are using pure Classic ASP, you can catch the `msxml6.dll` error `80072EE2` (“The operation timed out”). It’ll be thrown by the `send` method. – dakab Sep 03 '14 at 12:58