We help IT Professionals succeed at work.

JavaScript: Retry on HTTP GET Connection Timeout

ReneGe
ReneGe asked
on
816 Views
Last Modified: 2012-05-09
Hi there,

I need to add the feature of if the website is unavailable, to wait 10 seconds and try again. If my website is still unavailable, try again 5 times, if still navailable, echo "UnableToConnect" then exit.

IT WOULD LOGICALLY LOOK SOMETHING LIKE THIS (but in Java Script):

SET ConnectCounter=0
:Home
Echo http://www.MyWebSite.com
Send the GET command to the website and ECHO it out
If can't connect to http://www.MyWebSite.com  (
   If %ConnectCounter% GEQ 5 (
   ECHO UnableToConnect
   EXIT
   )
  SET /a ConnectCounter=%ConnectCounter%+1
  Add a delay of 10Seconds
)
Goto home


HERE IS MY ORIGINAL WORKING JAVA SCRIPT:
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo("http://www.MyWebSite.com");
request.open( "GET", "http://www.MyWebSite.com" , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}



Thanks for your help,
Rene
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
For my solution you need to replace the picture https://www.experts-exchange.com/expert/images/logo.jpg with your picture http://www.MyWebSite.com/path/to/a/pic.jpg

Author

Commented:
leakim971,

Thanks for your reference.

I tried to modify my code with yours without success.

Could you please help me with this?

This script is to monitor my website and I used Wikipedia website for the purpose of this post.

Thanks,
Rene


COMMAND LINE FROM MY BATCH FILE TO LAUNCH THE JAVA SCRIPT:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia


JAVA SCRIPT MODIFIED AS PER leakim971's LINK :
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

      var counter = 0;
      function timer() {
                var objArgs = WScript.Arguments;
                WScript.Echo(objArgs(0));
            request.open( "GET", objArgs(0) , true );
                request.send(null);
            setTimeout("timer()", 1000); // try to reload the picture every 1 seconds.
      }
      function resetCounter() {
            counter = 0;
      }
      function increaseCounter() {
            counter++;
            // If we fail to reload the pic 5 times, display alert message
            if(counter>5) alert("Your browser appears to be offline.\nPlease check your internet connection and try again.");
            
      }


while( notyetready )
{
WScript.Sleep( 100 );
}


leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Why replace ? Use the provided code entirely ? Or what does it miss?

Author

Commented:
I tried it but it did not work.
I need to call your script with a batch file, adding as an argument, the website to be monitored, and have the web site dumped in a text file for analysis.

FOR EXAMPLE:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt

Have any ideas?

Thanks,
Rene
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
>cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt

Not with javascript : You cannot run javasript from command line.
You need a VBScript

Author

Commented:
Hmmmmm

When I call the following JavaScript from a cammand line, it works.

Am I missing something here?


COMMAND LINE: cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt


JAVA SCRIPT (NAME: Monitor.js):

var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
WScript is VBScript not javascript. You can run VBScript from command line (with or without cscript) but not javascript.

Author

Commented:
Ok, now I am confised. The script I provided is a WScript, and that is also called VBScript?
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
leakim971,

I posted it as WScript and got it done.

Thanks for your help,
Rene

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.