Adobe Air how to check if URL is online\\gives any

2020-03-27 01:33发布

问题:

I have url I want to check if it is live. I want to get bool value. How to do such thing?

回答1:

You can use an URLLoader and listen for the events to check if it loads, and if not what might be the problem. Would be handy to use the AIRMonitor first to make sure the client's computer is online in the first place.

Here is a class I started to write to illustrate the idea:

package  
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.HTTPStatusEvent;
    import flash.events.IEventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    /**
     * ...
     * @author George Profenza
     */
    public class URLChecker extends EventDispatcher
    {
        private var _url:String;
        private var _request:URLRequest;
        private var _loader:URLLoader;
        private var _isLive:Boolean;
        private var _liveStatuses:Array;
        private var _completeEvent:Event;
        private var _dispatched:Boolean;
        private var _log:String = '';

        public function URLChecker(target:IEventDispatcher = null) 
        {
            super(target);
            init();
        }

        private function init():void
        {
            _loader = new URLLoader();
            _loader.addEventListener(Event.COMPLETE, _completeHandler);
            _loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, _httpStatusHandler);
            _loader.addEventListener(IOErrorEvent.IO_ERROR, _ioErrorEventHandler);
            _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _securityErrorHandler);

            _completeEvent = new Event(Event.COMPLETE, false, true);

            _liveStatuses = [];//add other acceptable http statuses here
        }

        public function check(url:String = 'http://stackoverflow.com'):void {
            _dispatched = false;
            _url = url;
            _request = new URLRequest(url);
            _loader.load(_request);
            _log += 'load for ' + _url + ' started : ' + new Date() + '\n';
        }

        private function _completeHandler(e:Event):void 
        {
            _log += e.toString() + ' at ' + new Date();
            _isLive = true;
            if (!_dispatched) {
                dispatchEvent(_completeEvent);
                _dispatched = true;
            }
        }

        private function _httpStatusHandler(e:HTTPStatusEvent):void 
        {
            /* comment this in when you're sure what statuses you're after
            var statusesLen:int = _liveStatuses.length;
            for (var i:int = statusesLen; i > 0; i--) {
                if (e.status == _liveStatuses[i]) {
                    _isLive = true;
                    dispatchEvent(_completeEvent);
                }
            }
            */
            //200 range
            _log += e.toString() + ' at ' + new Date();
            if (e.status >= 200 && e.status < 300) _isLive = true;
            else                                   _isLive = false;
            if (!_dispatched) {
                dispatchEvent(_completeEvent);
                _dispatched = true;
            }
        }

        private function _ioErrorEventHandler(e:IOErrorEvent):void 
        {
            _log += e.toString() + ' at ' + new Date();
            _isLive = false;
            if (!_dispatched) {
                dispatchEvent(_completeEvent);
                _dispatched = true;
            }
        }

        private function _securityErrorHandler(e:SecurityErrorEvent):void 
        {
            _log += e.toString() + ' at ' + new Date();
            _isLive = false;
            if (!_dispatched) {
                dispatchEvent(_completeEvent);
                _dispatched = true;
            }
        }

        public function get isLive():Boolean { return _isLive; }

        public function get log():String { return _log; }

    }

}

and here's a basic usage example:

var urlChecker:URLChecker = new URLChecker();
urlChecker.addEventListener(Event.COMPLETE, urlChecked);
urlChecker.check('wrong_place.url');

function urlChecked(event:Event):void {
    trace('is Live: ' + event.target.isLive);
    trace('log: ' + event.target.log);
}

The idea is simple: 1. You create a checked 2. Listen for the COMPLETE event(triggered when it has a result 3. In the handler check if it's live and what it logged.

In the HTTP specs, 200 area seems ok, but depending on what you load, you might need to adjust the class. Also you need to handle security/cross domain issue better, but at least it's a start.

HTH



回答2:

An important consideration that George's answer left out is the URLRequestMethod. If one were trying to verify the existence of rather large files (e.g, media files) and not just a webpage, you'd want to make sure to set the method property on the URLRequest to URLRequestMethod.HEAD.

As stated in the HTTP1.1 Protocol:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

Hence, if you really only want to verify the existence of the URL, this is the way to go.

For those who need the code spelled out:

var _request:URLRequest = URLRequest(url);
_request.method = URLRequestMethod.HEAD; // bandwidth :)

Otherwise, George's answer is a good reference point.

NB: This particular URLRequestMethod is only available in AIR.



标签: url air