flash中最普通的http请求是使用URLRequest类和navigateToURL()方法进行URL导航。
在创建HTTP请求时,需要URLRequest类参与处理所有的通信。
在创建HTTP请求之前,必须新建一个URLRequest对象:
var urlRequest:URLRequest = new URLRequest();
另外,URLRequest类在构造方法中有一个可选的参数用于指定HTTP请求的URL:
var urlRequest:URLRequest = new URLRequest("http://www.websote.com");
新建一个URLRequest对象之后,就乐意将其传递至任何需要HTTP请求对象的方法。
navigateToURL(urlRequest);
调用navigateToURL()后将会在同一个浏览器窗口中打开一个新的网页,若要在新的标签页或者窗口中浏览,需要指定方法的第二个参数。以下的代码通过指定第二个参数为_blank在新的窗口中打开网址:
navigateToURL(urlRequest,"_blank");
创建一个URLLoader对象
var ulLoader:URLLoader = new URLLoader();
将外部文件加载进URLLoader对象之前,需要创建一个URLRequest对象处理HTTP请求,并且使用该类的load方法加载文件:
var urRequest:URLRequest = new URLRequest('http://www.website.com/');
var ulLoader:URLLoader = new URLLoader();
urLoader.load(urRequest);
监听URLLoader对象的COMPLETE事件
var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');
var ulLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,loaderComleteHandler);
function loaderComleteHandler(evt:Event):void{
trace(evt.target.data);
}
ulLoader.load(urRequest);
var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');
var ulLoader:URLLoader = new URLLoader();
ulLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
ulLoader.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
ulLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
ulLoader.load(urRequest);
function loaderErrorHandler(evt:IOErrorEvent):void
{
trace(evt.text);
}
function loaderProgressHandler(evt:ProgressEvent):void{
var bloaded:Number = evt.target.bytesLoaded;
var bTotal:Number = evt.target.bytesTotal;
trace(bloaded + '/' + bTotal + 'has loaded');
}
function loaderCompleteHandler(evt:Event):void{
trace(evt.target.data);
}
var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');
var ulLoader:URLLoader = new URLLoader();
var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth;
tfDisplay.height = stage.stageHeight;
tfDisplay.wordWrap = true;
tfDisplay.border = true;
ulLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
ulLoader.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
ulLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
ulLoader.load(urRequest);
addChild(tfDisplay);
function loaderErrorHandler(evt:IOErrorEvent):void
{
trace(evt.text);
}
function loaderProgressHandler(evt:ProgressEvent):void{
var bloaded:Number = evt.target.bytesLoaded;
var bTotal:Number = evt.target.bytesTotal;
trace(bloaded + '/' + bTotal + 'has loaded');
}
function loaderCompleteHandler(evt:Event):void{
tfDisplay.htmlText = evt.target.data;
}
var cssRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/style.css');
var cssLoader:URLLoader = new URLLoader();
var cssSheet:StyleSheet = new StyleSheet();
var textSample:String = 'Me and Adrian
'
var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth();
tfDisplay.height = stage.stageHeight();
tfDisplay.wordWrap = true;
tfDisplay.border = true;
cssLoader.addEventListener(Event.COMPLETE,StyleTextHandler);
cssLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function StyleTextHandler(eve:Event):void{
cssSheet.parseCSS(evt.target.data);
tfDisplay.StyleSheet = cssSheet;
tfDisplay.htmlText = textSample;
addChild(tfDisplay);
}
function loaderErrorHandler(evt:IOErrorEvent):void
{
tfDisplay.htmlText = 'There was a problem while loading the content styles.Please try again.';
}
cssLoader.load(cssRequest);
var cssRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/style.css');
var cssLoader:URLLoader = new URLLoader();
var cssSheet:StyleSheet = new StyleSheet();
var textSample:String = 'Me and Adrian
'
var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth();
tfDisplay.height = stage.stageHeight();
tfDisplay.wordWrap = true;
tfDisplay.border = true;
cssLoader.addEventListener(Event.COMPLETE,StyleTextHandler);
cssLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function StyleTextHandler(eve:Event):void{
cssSheet.parseCSS(evt.target.data);
tfDisplay.StyleSheet = cssSheet;
tfDisplay.htmlText = textSample;
addChild(tfDisplay);
}
function loaderErrorHandler(evt:IOErrorEvent):void
{
tfDisplay.htmlText = 'There was a problem while loading the content styles.Please try again.';
}
cssLoader.load(cssRequest);
var assetLoader:Loader = new Loader();
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function loaderCompleteHandler(evt:Event):void
{
trace('Image has loaded.');
addChild(assetLoader);
}
function loaderErrorHandler(evt:IOErrorEvent):void{
trace('An error has occurred while loading the image');
}
assetLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
var assetLoader:Loader = new Loader();
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function loaderCompleteHandler(evt:Event):void
{
trace('Image has loaded.');
addChild(assetLoader);
stage.addEventListener(MouseEvent.MOUSE_UP,unloadAssetHandler);
}
function unloadAssetHandler(evt:MouseEvent):void
{
assetLoader.unload();
stage.removeEventListener(MouseEvent.MOUSE_UP,unloadAssetHandler);
}
function loaderErrorHandler(evt:IOErrorEvent):void{
trace('An error has occurred while loading the image');
}
assetLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/external_movie.swf'));
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void{
addChild(imageLoader);
}
function loaderErrorHandler(evt:Event):void{
trace('An error has occurred while loading the image');
}
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
function loaderProgressHandler(evt:ProgressEvent):void{
var percentage:Number = evt.bytesLoaded/evt.bytesTotal;
trace('Percentage'+ percentage);
}
imageLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
addChild(imageLoader);
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderProgressHandler(evt:ProgressEvent):void{
var percentage:Number = evt.bytesLoaded/evt.bytesTotal;
trace('Percentage:'+percentage);
}
function loaderCompleteHandler(evt:Event):void
{
removeChild(loadDisplay);
}
imageLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
addChild(imageLoader);
var swfContainer:MovieClip;
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void
{
swfContainer = swfLoader.content as MovieClip;
addChild(swfContainer);
}
swfLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/external_movie.swf'));
var swfContainer:MovieClip;
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void
{
swfContainer = swfLoader.content as MovieClip;
addChild(swfContainer);
swfContainer.setText('Loaded SWF');
}
swfLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/external_movie.swf'));