5 Replies Latest reply on May 29, 2009 11:21 AM by Jeff Swartz

    Best solution for domain or url check

    Rossipooh

      Hi

       

      I just started using AIR and am developing a simple app to check the availability of a list of domains. There seems to be several options using the AIR api and I've been working on the code below.The core of it was taken from an example in the AIR docs.

       

      All I need is to have the HTTP status code returned (200, 400, etc.).

       

      URLMonitor looked like the way to go but I noted that on sites like Amazon it returned a 405 code instead of a 200. Other methods return a 200.

       

      The problem I have with the code below is this: the  check_url_exists function calls the loader.load method and we wait for the httpStatusHandler to return the HTTPStatusEvent. Problem is the loop that's checking the url array continues to loop resulting in reversed and confused output. Obviously the functions called by the loader events are executing while the check_url_exists loop is executing.

       

      So, is this the best solution for my needs, and if so, how can I get the loader event to finish before the next loop iteration proceeds?

       

      Thanks in advance.

       

      Note: I'm not using Flex.

       

      -------------------------------------------------------------------------------------

      var urls=[];
      urls[0]="http://www.amazon.com";
      urls[1]="http://www.google.com";
      urls[2]="http://www.allmusic.com";
      urls[3]="http://www.this_is_not_a_valid_domain.com";

       

      var loader = new air.URLLoader();

       

      configureListeners(loader);

       

      function configureListeners(dispatcher) {
        dispatcher.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        dispatcher.addEventListener(air.HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(air.IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
                             
      function securityErrorHandler(event) {
          air.trace("securityErrorHandler: " + event);
              }

       

      function httpStatusHandler(event) {
          air.trace("httpStatusCode: " + event.status + " - ");
              }

       

      function ioErrorHandler(event) {
          air.trace("ioErrorHandler: " + event.errorID + " - ");
              }

       

      for(var x=0;x<urls.length;x++){
        check_url_exists(urls[x],loader);
          }

       

      function check_url_exists(url,loader) {

       

        var request = new air.URLRequest(url);
        request.authenticate=false;           
        request.method = air.URLRequestMethod.HEAD;
        loader.load(request);
        }

        • 1. Re: Best solution for domain or url check
          Jeff Swartz Level 3

          The following JavaScript code processes one URL at a time:

           

          var urls=[];
          urls[0]="http://www.amazon.com";
          urls[1]="http://www.google.com";
          urls[2]="http://www.allmusic.com";
          urls[3]="http://www.this_is_not_a_valid_domain.com";
          var urlIndex = 0;

           

          var loader = new air.URLLoader();
          configureListeners(loader);
           
          function configureListeners(dispatcher) {
            dispatcher.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(air.HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(air.IOErrorEvent.IO_ERROR, ioErrorHandler);
          }
                               
          function securityErrorHandler(event) {
              air.trace("securityErrorHandler: " + event);
              checkNextURL();
          }

           

          function httpStatusHandler(event) {
              air.trace("httpStatusCode: " + event.status + " - ");
              checkNextURL();
          }

           

          function ioErrorHandler(event) {
              air.trace("ioErrorHandler: " + event.errorID + " - ");
          }

           

          checkNextURL();
          function checkNextURL() {
            if (urlIndex < urls.length) {
              air.trace(urls[urlIndex]);
              check_url_exists(urls[urlIndex]);
              urlIndex++;
            }
          }

           

          function check_url_exists(url) {
            var request = new air.URLRequest(url);
            request.authenticate=false;          
            request.method = air.URLRequestMethod.HEAD;
            loader.load(request);
          }

           

          I hope this helps.

          • 2. Re: Best solution for domain or url check
            Jeff Swartz Level 3

            And here's a way you can use a for loop and have multiple asynchronous url checks:

             

            var urls=[];
            urls[0]="http://www.amazon.com";
            urls[1]="http://www.google.com";
            urls[2]="http://www.allmusic.com";
            urls[3]="http://www.this_is_not_a_valid_domain.com";

             

            var urlRequest;

             

            function configureListeners(dispatcher, request) {
              dispatcher.addEventListener(
                            air.SecurityErrorEvent.SECURITY_ERROR,
                            function(event) {
                              air.trace("url:", request.url, "- securityErrorHandler: " + event);
                            }
              );
              dispatcher.addEventListener(
                            air.HTTPStatusEvent.HTTP_STATUS,
                            function(event) {
                              air.trace("url:", request.url, "- securityErrorHandler: " + event);
                            }
              );
              dispatcher.addEventListener(
                            air.IOErrorEvent.IO_ERROR,
                            function(event) {
                              air.trace("url:", request.url, "- securityErrorHandler: " + event);
                            }
              );

             

            }
                                 
            function securityErrorHandler(event) {
                air.trace("url:", urlRequest, "- securityErrorHandler: " + event);
            }

             

            function httpStatusHandler(event) {
                air.trace("url:", urlRequest, "- httpStatusCode: " + event.status + " - ");
            }

             

            function ioErrorHandler(event) {
                air.trace("url:", urlRequest, "- ioErrorHandler: " + event.errorID + " - ");
            }

             

            for(x=0; x<urls.length; x++) {
                check_url_exists(urls[x]);
            }

             

            function check_url_exists(url) {
              var loader = new air.URLLoader();
              var request = new air.URLRequest(url);
              configureListeners(loader, request);
             
              request.authenticate=false;          
              request.method = air.URLRequestMethod.HEAD;
              loader.load(request);
            }

             

            That said, I think URLMonitor is a good solution. Amazon returns a 405 status code when you use a HEAD URL request method. You can add this code to the list of acceptable status codes:

             

            var urls=[];
            urls[0]="http://www.amazon.com";
            urls[1]="http://www.google.com";
            urls[2]="http://www.allmusic.com";
            urls[3]="http://www.this_is_not_a_valid_domain.com";

             


            for(i=0; i < urls.length; i++) {
              var request = new air.URLRequest(urls[i]);
              request.authenticate=false;          
              request.method = air.URLRequestMethod.HEAD;
             
              var acceptableStatusCodes= [200, 202, 204, 205, 206, 405];
              var monitor = new runtime.air.net.URLMonitor(request, acceptableStatusCodes);
              monitor.addEventListener(air.StatusEvent.STATUS, traceDetails);
              monitor.start()
            }
            function traceDetails(event) {
              var monitor = event.target;
              air.trace("url:", monitor.urlRequest.url, ": " + event);
            }

            1 person found this helpful
            • 3. Re: Best solution for domain or url check
              Rossipooh Level 1

              Thanks, Jeff, that's made it all very clear.

               

              One further thing: I would like to have the status event also return the url that the event refers to. If I trace event.target I get a ref to the loader object. But there doesn't seem to be a way to reference the url property of that loader.

               

              Any thoughts?

               

              Thanks again.

               

              Ross

              • 4. Re: Best solution for domain or url check
                Rossipooh Level 1

                Ok, found a solution. Kinda stumbled upon it.

                 

                Change the HTTPStatusEvent to HTTP_RESPONSE_STATUS

                That gives access to the responseURL property of the event

                 

                Now, that's fine for the status event handler, but returns undefined for the error handler.

                 

                But the error event has a text property (string) which the referring url can be extracted from.

                 

                That seems like a fix.

                 

                Thanks, again, Jeff.

                 

                ------

                 

                function configureListeners(dispatcher) {
                  dispatcher.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                  dispatcher.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);
                  dispatcher.addEventListener(air.IOErrorEvent.IO_ERROR, ioErrorHandler);
                }
                                    
                function securityErrorHandler(event) {
                    air.trace("securityErrorHandler: " + event);
                    checkNextURL();
                }

                 

                function httpStatusHandler(event) {
                    air.trace("httpStatusCode: " + event.status + " - " + event.responseURL);
                    checkNextURL();
                }

                 

                function ioErrorHandler(event) {
                    air.trace("ioErrorHandler: " + event.errorID + " - " + event.text);
                }

                 

                ------

                • 5. Re: Best solution for domain or url check
                  Jeff Swartz Level 3

                  Note that if you use URLMonitor, you can get the URL from the status event this way:

                   

                    var monitor = event.target;
                    air.trace("url:", monitor.urlRequest.url);

                   

                  Jeff