gpt4 book ai didi

url - Adobe Air 如何检查 URL 是否在线\给出任何响应存在?

转载 作者:行者123 更新时间:2023-12-02 08:58:48 24 4
gpt4 key购买 nike

我有一个网址,我想检查它是否有效。我想获得 bool 值。怎么做这样的事情?

最佳答案

您可以使用 URLLoader 并监听事件来检查它是否加载,如果没有加载,可能是什么问题。首先使用 AIRMonitor 来确保客户的计算机处于在线状态会很方便。

这是我开始编写的一个类来说明这个想法:

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

}

}

这是一个基本用法示例:

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. 在处理程序中检查它是否处于事件状态以及记录的内容。

HTTP specs , 200 区域似乎没问题,但根据您加载的内容,您可能需要来调整类(class)。此外,您还需要更好地处理安全/跨域问题,但至少这是一个开始。

HTH

关于url - Adobe Air 如何检查 URL 是否在线\给出任何响应存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2891656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com