Adobe AIR的如何检查网址在线\给出任何回应的存在? [英] Adobe Air how to check if URL is online\gives any response exists?

查看:126
本文介绍了Adobe AIR的如何检查网址在线\给出任何回应的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网址我要检查,如果它是活的。我想获得布尔值。如何做这样的事?

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

推荐答案

您可以使用URLLoader和监听事件来检查它是否加载,如果没有可能是什么问题。将方便使用AIRMonitor第一,以确保客户端的计算机联机摆在首位。

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);
}

这个想法很简单: 1.创建一个检查 2.侦听complete事件(触发时,它有一个结果 3.在处理程序检查它是否是生活和它所记录的。

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.

HTTP规范的,200地区似乎确定,但根据在你加载,则可能需要 调节类。另外你需要处理的安全性/跨域问题更好,但至少它是一个开始。

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.

心连心

这篇关于Adobe AIR的如何检查网址在线\给出任何回应的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆